Swing JAVA中的简单窗口 [英] Simple window in swing JAVA
本文介绍了Swing JAVA中的简单窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任务:使用 JAVA swing 创建与下面屏幕截图相同的窗口:
Task: make the same window as in the screenshot below using JAVA swing:
我做了什么:
- 为顶部块(BorderLayout)创建了一个面板,向其添加了另外两个面板(GridLayour),一个用于左侧按钮(FR、FG、FB),另一个用于右侧按钮(A、B、C),将其全部添加到我的 JFrame 窗口中
- 创建了一个 JScrollPane 并将其添加到 JFrame 中
- 为底部块(BorderLayout)创建了一个面板,向其添加了另外两个面板(GridLayour),一个用于左侧按钮(1,2,3,4...),另一个用于 JTextFiel 文本字段,将其全部添加到我的 JFrame 窗口中.
结果如下:
我尝试使用其他布局,但仍然不起作用.我附上代码.
I tried using other layouts, but it still doesn't work. I attach the code.
import javax.swing.*;
import java.awt.*;
public class MyJFrame extends JFrame {
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();
JPanel pan3 = new JPanel();
JPanel pan4 = new JPanel();
JPanel pan5 = new JPanel();
JPanel pan6 = new JPanel();
JButton jButton1 = new JButton("FR");
JButton jButton2 = new JButton("FG");
JButton jButton3 = new JButton("FB");
JButton jButton4 = new JButton("A");
JButton jButton5 = new JButton("B");
JButton jButton6 = new JButton("C");
public MyJFrame(){
super("Simple Swing App");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(650,300);
setLayout(new GridLayout(3,2));
setResizable(true);
JScrollPane scrollPane = new JScrollPane();
jButton1.setBackground(Color.red);
jButton2.setBackground(Color.green);
jButton3.setBackground(Color.blue);
pan1.setLayout(new GridLayout(1,3,2,2));
pan2.setLayout(new GridLayout(1,3,2,2));
pan3.setLayout(new BorderLayout());
pan4.setLayout(new GridLayout(3,3,2,2));
pan5.setLayout(new GridLayout(3,1,1,1));
pan6.setLayout(new BorderLayout());
pan1.add(jButton1);
pan1.add(jButton2);
pan1.add(jButton3);
pan2.add(jButton4);
pan2.add(jButton5);
pan2.add(jButton6);
pan3.add(pan1, BorderLayout.WEST);
pan3.add(pan2, BorderLayout.EAST);
for (int i=1; i<10; i++) {
JButton jButton = new JButton(i+"");
pan4.add(jButton);
}
for (int i=1; i<4; i++){
JTextField jTextField = new JTextField(" Pole tekstowe " + i + " typu jTextField ");
jTextField.setBackground(Color.WHITE);
jTextField.setBorder(BorderFactory.createLineBorder(Color.CYAN));
pan5.add(jTextField);
}
pan6.add(pan4, BorderLayout.WEST);
pan6.add(pan5, BorderLayout.EAST);
add(pan3);
add(scrollPane);
add(pan6);
setSize(700,450);
setVisible(true);
}
}
推荐答案
如果问题是如何制作这个 GUI?"我会使用这种方法:
If the question is "How to make this GUI?" I would use this approach:
- 3 x
BorderLayout
(红色) - 一个用于整个 GUI,每个用于PAGE_START
和PAGE_END
主 GUI 面板的约束. - 在
PAGE_START
中使用的面板中,2 xFlowLayout
(绿色),LINE_START
中的一个,LINE_END
中的另一个.(1) - 在
PAGE_END
面板中,2 xGridLayout
(blue),第一个是3 x 3,另一个是单列.
- 3 x
BorderLayout
(red) - one for the entire GUI, one each for thePAGE_START
andPAGE_END
constraints of the main GUI panel. - In the panel used in the
PAGE_START
, 2 xFlowLayout
(green), one in theLINE_START
, the other in theLINE_END
. (1) - In the panel in the
PAGE_END
, 2 xGridLayout
(blue), the first a 3 x 3, the other a single column.
- 如果顶部的组件(左侧和右侧的按钮组)需要完全相同的大小,也可以为它们使用网格布局.
这篇关于Swing JAVA中的简单窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文