我正在关注this教程并设置我的GUI。我想要一个textarea
应该占据整个宽度(所有列),但它不会发生,屏幕就像这样:
此青色字段应捕获整个屏幕的宽度。以下是我的代码:
代码
package com.company.app;
import javax.swing.*;
import java.awt.*;
public class SpamGUI {
public static void main(String[] args) {
System.out.println("Loading Program..");
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JFrame frame = new JFrame("Evil App");
frame.setSize(800, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setResizable(false);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 3));
//panel.setLayout(new GridLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
/**
* Training File Section Path
*/
JLabel lblTrainPath = new JLabel("Enter Training Folder Path");
lblTrainPath.setSize(100, 10);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(lblTrainPath, c);
JTextField txtTrainPath = new JTextField();
txtTrainPath.setSize(100, 20);
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 0;
c.gridy = 1;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(txtTrainPath, c);
JButton btnTrainPath = new JButton("Browse..");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.weightx = 0.5;
c.weighty = 0.5;
c.gridwidth = 3;
panel.add(btnTrainPath, c);
/**
* Testing File Section Path
*/
JLabel lblTestingPath = new JLabel("Enter Testing Folder Path");
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 1;
c.gridy = 0;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(lblTestingPath, c);
JTextField txtTestPath = new JTextField();
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 1;
c.gridy = 1;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(txtTestPath, c);
JButton btnTestPath = new JButton("Browse..");
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 1;
c.gridy = 2;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(btnTestPath, c);
/**
* Pogress Section
*/
JTextArea txtProgress = new JTextArea();
txtProgress.setBackground(Color.cyan);
txtProgress.setOpaque(true);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 0;
c.ipadx = 50;
c.weightx = 0.5;
c.weighty = 0.5;
c.gridwidth = 2;
panel.add(txtProgress, c);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
答案 0 :(得分:1)
gridx
/ gridy
订单被撤销。gridwidth
过早地随机设置为3。..这就是我记得的变化。
import javax.swing.*;
import java.awt.*;
public class SpamGUI {
public static void main(String[] args) {
System.out.println("Loading Program..");
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JFrame frame = new JFrame("Evil App");
//frame.setSize(800, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setResizable(false);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
//panel.setLayout(new GridLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
/**
* Training File Section Path
*/
JLabel lblTrainPath = new JLabel("Enter Training Folder Path");
//lblTrainPath.setSize(100, 10);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(lblTrainPath, c);
JTextField txtTrainPath = new JTextField(10);
//txtTrainPath.setSize(100, 20);
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 1;
c.gridy = 0;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(txtTrainPath, c);
JButton btnTrainPath = new JButton("Browse..");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 0;
c.weightx = 0.5;
c.weighty = 0.5;
//c.gridwidth = 3;
panel.add(btnTrainPath, c);
/**
* Testing File Section Path
*/
JLabel lblTestingPath = new JLabel("Enter Testing Folder Path");
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 0;
c.gridy = 1;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(lblTestingPath, c);
JTextField txtTestPath = new JTextField(10);
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 1;
c.gridy = 1;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(txtTestPath, c);
JButton btnTestPath = new JButton("Browse..");
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 2;
c.gridy = 1;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(btnTestPath, c);
/**
* Pogress Section
*/
JTextArea txtProgress = new JTextArea(5,15);
txtProgress.setBackground(Color.cyan);
txtProgress.setOpaque(true);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.ipadx = 50;
c.weightx = 0.5;
c.weighty = 0.5;
c.gridwidth = 3;
panel.add(txtProgress, c);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
GUI非常需要一些空白区域。改变一些改变:
c.fill = GridBagConstraints.HORIZONTAL;
要:
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(5,5,5,5);