简体   繁体   English

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

[英]How do I create the following GUI in Java Swing?

I want to create the following GUI with Java Swing. 我想用Java Swing创建以下GUI。

我想要的GUI

Since I'm not experienced enough with Java Swing, I'm not sure how to exactly recreate that GUI. 由于我对Java Swing没有足够的经验,我不确定如何精确地重新创建该GUI。

I've tried using GridLayout which looks like this: 我尝试过使用GridLayout,它看起来像这样:

网格布局

I've tried other LayoutManagers but due to my inexperience, I couldn't get anything even remotely resembling the GUI I want to achieve. 我已经尝试过其他的LayoutManagers,但由于我的经验不足,我无法获得任何与我想要实现的GUI类似的东西。

I probably have to use GridBagLayout but I've tried it and simply wasn't able to get anything done. 我可能不得不使用GridBagLayout,但我已经尝试过,而且根本无法完成任何事情。 I'm not sure how to exactly use GridBagLayout, especially since there is a variance of the amount of colums needed (2, 2 and then 3). 我不确定如何使用GridBagLayout,特别是因为所需的colums数量存在差异(2,2和3)。

Here is the code used for creating the second GUI: 以下是用于创建第二个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);
}
}

I'm thankful for any kind of support! 我很感谢任何支持!

You could try something like this: 你可以尝试这样的事情:

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);
   }
}

Explanation: 说明:

Firstly I created a main JPanel with a BorderLayout . 首先,我创建了一个带有BorderLayout的主JPanel This JPanel will be split horizontally, the CENTRE component will be another JPanel containing the text areas and labels, and the SOUTH component will be a JPanel containing the buttons. JPanel将水平分割, CENTRE组件将是包含文本区域和标签的另一个JPanel ,而SOUTH组件将是包含按钮的JPanel

The JPanel that contains the text areas is given a GridLayout so that it can be easily split vertically, and is also given a hgap of 10 to add some spacing. 包含文本区域的JPanel被赋予一个GridLayout以便它可以很容易地垂直分割,并且还给出了一个10hgap来添加一些间距。

The left and right JPanels that are put into that are both the same. 放入的左右JPanels都是相同的。 They have a BorderLayout with a vgap to add spacing. 他们有一个带有vgapBorderLayout来添加间距。 The NORTH component is a JLabel and the CENTRE component is a JScrollPane containing a JTextArea . NORTH组件是JLabelCENTRE组件是包含JTextAreaJScrollPane

Finally, the SOUTH component of the main JPanel is another JPanel which is given a BorderLayout again. 最后,主JPanelSOUTH组件是另一个JPanel ,再次给出了BorderLayout Three JButton s are added with WEST , CENTRE and EAST attributes allocated accordingly. 添加了三个JButton ,相应地分配了WESTCENTREEAST属性。

The overall result looks like: 整体结果如下:

在此输入图像描述

Here is your code with just some little changes :) 这是你的代码只有一些小的变化:)

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

     public class GUITest extends JFrame {

         public GUITest() {

              super("Testing Title");  
              Container pane = getContentPane();  
              pane.setLayout(new BorderLayout());//Modified Layout to BorderLayout  
              pane.add(getHeader(),BorderLayout.NORTH); //BorderLayout.NORTH
              pane.add(getTextArea(),BorderLayout.CENTER);//BorderLayout.CENTER
              pane.add(getButtonPanel(),BorderLayout.SOUTH);//BorderLayout.SOUTH

        }  

         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());//Modified to standard FlowLayout  
          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.pack(); //Modified setSize(700,500) to pack()  
          e.setVisible(true);  
          e.setResizable(false);  
          e.setDefaultCloseOperation(EXIT_ON_CLOSE);  
          e.setLocationRelativeTo(null);  
     }  
}  

GridLayout sizes all cells the same, ie your outer layout with 3 rows and 1 column makes 3 cells of all the same size. GridLayout大小相同的所有单元格,即您的外部布局有3行和1列,使3个单元格大小相同。

Instead, use BorderLayout for your outer container and add the top, mid and lower panels with constraints BorderLayout.NORTH, BorderLayout.CENTER and BorderLayout.SOUTH respectively 相反,使用BorderLayout作为外部容器,并分别添加限制BorderLayout.NORTH,BorderLayout.CENTER和BorderLayout.SOUTH的顶部,中间和下部面板

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

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