简体   繁体   English

Java Swing GUI布局

[英]Java Swing GUI layout

I am building a simple Swing app that I want to have text fields/ combo boxes / dropdowns / etc. going down the page from left to right. 我正在构建一个简单的Swing应用程序,我希望它具有文本字段/组合框/下拉菜单等。 I also want to be able size the textfields and combo boxes to the size of the text and no bigger, like on a web page. 我还希望能够将文本字段和组合框的大小调整为文本的大小,而不是更大,例如在网页上。

I have create a JFrame , which contains a list of panes. 我创建了一个JFrame ,其中包含窗格列表。 Currently the Search pane has a lable on the right, then the textfield, and then a search button which all appear vertically the full length of the screen. 当前,“搜索”窗格的右侧有一个标签,然后是文本字段,然后是一个搜索按钮,它们全部垂直出现在屏幕的整个长度上。

Here is the code: 这是代码:

public class SearchEvent {

public JPanel createSearchPane() {
    JTextField searchEvent = new JTextField();
    //searchEvent.setSize(10, 20);
    //searchEvent.setSize(new Dimension(10, 10));
    JPanel panel = new JPanel();
    GridLayout topLayout = new GridLayout(1,1);
    panel.setLayout(topLayout); 
    panel.add(new JLabel("Event  to search"));
    panel.add(searchEvent);
}
}

MainClass{

private static void createAndShowGUI() {
    JFrame frame = new JFrame("Search Event ");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane();

    tabbedPane.addTab("Search", null, new SearchEvent().createSearchPane(),"Search an event");
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

            //Display the window.
    frame.pack();
    frame.setVisible(true);
}

How can I make the text field appear on the right of the screen, the size of the text in the text field and the label to appear on the left beside the text field? 如何使文本字段显示在屏幕的右侧,文本字段中的文本大小以及标签显示在文本字段旁边的左侧?

Using springLayout I achieved what I wanted to do by the following 2 lines: 使用springLayout我通过以下两行代码实现了我想做的事情:

textField.setMinimumsize(tf.getPreferredsize()) textField.setMaximumSize(tf.getPreferredSize()) textField.setMinimumsize(tf.getPreferredsize())textField.setMaximumSize(tf.getPreferredSize())

For what i want to do im not sure its worth investing in the time to learn gridbaglayout 对于我想做的事,我不确定在学习gridbaglayout的时间上值得投资

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM