如何在Java Swing中创建以下GUI?

时间:2016-07-01 11:11:15

标签: java swing user-interface grid-layout

我想用Java Swing创建以下GUI。

GUI I want to have

由于我对Java Swing的经验不足,我不确定如何完全重新创建该GUI。

我尝试过使用GridLayout,如下所示:

GridLayout

我尝试过其他的LayoutManagers,但由于我的经验不足,我甚至无法获得任何与我想要实现的GUI类似的东西。

我可能不得不使用GridBagLayout,但我已经尝试过,而且根本无法完成任何事情。 我不确定如何使用GridBagLayout,特别是因为所需的colums数量存在差异(2,2和3)。

以下是用于创建第二个GUI的代码:

import java.awt.*;
import javax.swing.*;

public class GUITest extends JFrame {

public GUITest() {
    super("Testing Title");
    Container pane = getContentPane();

    pane.setLayout(new GridLayout(3,1));

    pane.add(getHeader());
    pane.add(getTextArea());
    pane.add(getButtonPanel());

}

public JComponent getHeader() {
    JPanel labelPanel = new JPanel();
    labelPanel.setLayout(new GridLayout(1,2));
    labelPanel.setSize(getPreferredSize());

    JLabel labelLocal = new JLabel("Left value: ", JLabel.CENTER);
    JLabel labelDB = new JLabel("Right value: ", JLabel.CENTER);

    labelPanel.add(labelLocal);
    labelPanel.add(labelDB);

    return labelPanel;
}

public JComponent getTextArea() {
    JPanel textPanel = new JPanel();
    textPanel.setLayout(new GridLayout(1,2,5,0));

    JTextArea testTextArea = new JTextArea();
    testTextArea.setEditable(false);
    JScrollPane sp1 = new JScrollPane(testTextArea); 

    JTextArea testTextArea2 = new JTextArea();
    JScrollPane sp2 = new JScrollPane(testTextArea2); 
    testTextArea2.setEditable(false);

    testTextArea.setText("Hello Hello Hello\nTesting!\ntesterino\ntesteroni");
    testTextArea2.setText("Hello Hello Hello\nTesting!\ntest\nABC123\ncdef123\nhijk123");

    textPanel.add(sp1);
    textPanel.add(sp2);
    return textPanel;
}

public JComponent getButtonPanel() {
    JPanel inner = new JPanel();
    inner.setLayout(new FlowLayout((FlowLayout.CENTER),0,100));
    inner.add(new JButton("Do something"));
    inner.add(new JButton("Do something different"));
    inner.add(new JButton("Do something even more different"));
    return inner;
}

public static void main(String[] args) {
    GUITest e = new GUITest();
    e.setSize(700, 500);
    e.setVisible(true);
    e.setResizable(false);
    e.setDefaultCloseOperation(EXIT_ON_CLOSE);
    e.setLocationRelativeTo(null);
}
}

我很感谢任何支持!

3 个答案:

答案 0 :(得分:8)

您可以尝试这样的事情:

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class Example {

   public static void main(String[] args) {

       JFrame jFrame = new JFrame();
       jFrame.setTitle("Testing Title");
       jFrame.setLocationRelativeTo(null);

       JPanel mainPanel = new JPanel(new BorderLayout());
       mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

       JPanel listPanel = new JPanel(new GridLayout(0, 2, 10, 0));

       JPanel leftListPanel = new JPanel(new BorderLayout(0, 10));
       JLabel leftLabel = new JLabel("Left value:");
       JTextArea leftTextArea = new JTextArea("Hello Hello Hello\nTesting!\ntest");
       JScrollPane leftScrollPane = new JScrollPane(leftTextArea);
       leftListPanel.add(leftLabel, BorderLayout.NORTH);
       leftListPanel.add(leftScrollPane, BorderLayout.CENTER);

       JPanel rightListPanel = new JPanel(new BorderLayout(0, 10));
       JLabel rightLabel = new JLabel("Right value:");
       JTextArea rightTextArea = new JTextArea("Hello Hello Hello\nTesting!\ntest");
       JScrollPane rightScrollPane = new JScrollPane(rightTextArea);
       rightListPanel.add(rightLabel, BorderLayout.NORTH);
       rightListPanel.add(rightScrollPane, BorderLayout.CENTER);

       listPanel.add(leftListPanel);
       listPanel.add(rightListPanel);
       mainPanel.add(listPanel, BorderLayout.CENTER);

       JPanel buttonsPanel = new JPanel(new BorderLayout());
       buttonsPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
       buttonsPanel.add(new JButton("Do something"), BorderLayout.WEST);
       buttonsPanel.add(new JButton("Do something different"), BorderLayout.CENTER);
       buttonsPanel.add(new JButton("Do something even more different"), BorderLayout.EAST);
       mainPanel.add(buttonsPanel, BorderLayout.SOUTH);

       jFrame.setContentPane(mainPanel);
       jFrame.pack();
       jFrame.setVisible(true);
   }
}

说明:

首先,我创建了一个带有JPanel的主BorderLayout。此JPanel将水平拆分,CENTRE组件将是包含文本区域和标签的另一个JPanelSOUTH组件将包含JPanel按钮。

包含文本区域的JPanel被赋予GridLayout,以便可以轻松地垂直拆分,并且还会hgap 10添加一些间距。

放入的左右JPanels都是相同的。他们有BorderLayout vgap来添加间距。 NORTH组件为JLabelCENTRE组件为JScrollPane,其中包含JTextArea

最后,主SOUTH的{​​{1}}组件是另一个JPanel,再次获得JPanel。添加了三个BorderLayout,并相应地分配了JButtonWESTCENTRE个属性。

整体结果如下:

enter image description here

答案 1 :(得分:5)

以下是您的代码,只有一些小改动:)

where()

答案 2 :(得分:4)

GridLayout将所有单元格的大小相同,即您的外部布局包含3行和1列,使3个单元格大小相同。

相反,使用BorderLayout作为外部容器,并分别添加具有约束BorderLayout.NORTH,BorderLayout.CENTER和BorderLayout.SOUTH的顶部,中间和下部面板