【发布时间】:2016-11-24 00:08:49
【问题描述】:
我在这里阅读了很多主题,但我无法按照我想要的布局制作我的窗口。 我只是希望我的所有图形对象都像这里的第一张图片一样排成一行:http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html
我尝试过 GridLayout,但它仍然使我的第一个按钮变大,然后,随着我添加文本字段,它变得越来越小?!
这是我没有所有导入的代码:
public class TestScrollPane extends JFrame implements ActionListener{
Dimension dim = new Dimension(200 , 50);
JButton button;
JPanel panel = new JPanel();
JScrollPane scrollpane = new JScrollPane(panel);
public TestScrollPane(){
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
this.add(scrollpane);
//panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
this.setSize(300, 400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
button = new JButton("click me");
button.setPreferredSize(dim);
panel.add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == button ){
JTextField txt = new JTextField(); // we add a new button
txt.setPreferredSize(dim);
panel.add(txt);
SwingUtilities.updateComponentTreeUI(this); // refresh jframe
}
}
public static void main(String[] args){
TestScrollPane test = new TestScrollPane();
}
}
我只想每行有一个按钮。
【问题讨论】:
标签: java swing jpanel jscrollpane