Swing中创建以下GUI

Swing中创建以下GUI

本文介绍了如何在Java Swing中创建以下GUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Java Swing创建以下GUI.

I want to create the following GUI with Java Swing.

由于我对Java Swing的了解不足,所以我不确定如何准确地重新创建该GUI.

Since I'm not experienced enough with Java Swing, I'm not sure how to exactly recreate that GUI.

我尝试使用如下所示的GridLayout:

I've tried using GridLayout which looks like this:

我尝试了其他LayoutManager,但是由于我的经验不足,我什至无法获得与我想要实现的GUI类似的任何东西.

I've tried other LayoutManagers but due to my inexperience, I couldn't get anything even remotely resembling the GUI I want to achieve.

我可能必须使用GridBagLayout,但是我已经尝试过了,但是根本无法完成任何事情.我不确定如何准确地使用GridBagLayout,尤其是因为所需的列数量存在差异(分别是2、2,然后是3).

I probably have to use GridBagLayout but I've tried it and simply wasn't able to get anything done.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).

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

Here is the code used for creating the second 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);
}
}

我非常感谢您的支持!

推荐答案

这是您的代码,只做了一些小改动:)

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

这篇关于如何在Java Swing中创建以下GUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:35