使用Swing时,组件不会显示在我的JFrame
中。 其实我的宗旨是:如何使用Swing在JFrame中添加面板
- 添加边框
- 在帧面板中添加
- 面板cantains 3个按钮
但它并没有显示出来。
这是我的代码
public class Panels
{
JFrame frame;
JPanel panel;
private JButton addButton;
private JButton modifyButton;
private JButton deleteButton;
Panels()
{
initGUI();
launchFrame();
}
public void initGUI()
{
frame = new JFrame();
panel = new JPanel();
addButton = new JButton("Add");
modifyButton = new JButton("Modify");
deleteButton = new JButton("Delete");
}
public void launchFrame()
{
addButton.setBounds(130,50,225,25);
addButton.setBounds(150,50,225,25);
addButton.setBounds(170,50,225,25);
addButton.setBounds(190,50,225,25);
panel.add(addButton);
panel.add(modifyButton);
panel.add(deleteButton);
panel.setLayout(null);
panel.setBackground(Color.RED);
frame.add(panel);
frame.setTitle("My Frame with Panel");
frame.setSize(600,400);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
下面是主要用于调用面板类
当在主功能帧运行被示出没有控制器(即3个按钮未示出)
public class Main
{
public static void main(String[] args)
{
Panels obj_panel=new Panels();
}
}
也许尝试在设置边界之前调用set layout null。 – Arvy 2014-09-06 08:47:16
没有先生不工作 – 2014-09-06 08:48:26
在这段代码中有任何问题? – 2014-09-06 08:51:50