好的,我正在为我正在制作的程序创建一个菜单/ GUI但是我没有太多的经验,所以我需要一些关于组件组织和布局的帮助。
现在,当我运行此代码时,正如您在图片中看到的那样(抱歉,我没有足够的代表来嵌入它,新的stackoverflow)JTextField和它之间有一个很大的空间。以前的按钮/窗口边框是。如果有人可以帮我解决这个问题,那就太好了。 :) 感谢。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class WindowWin extends JFrame implements ActionListener
{
JPanel[] row = new JPanel[4];
JButton[] button = new JButton[4];
String[] buttonString = {"Copy to Clipboard","Go","Back","Info"};
int[] dimW = {400,200,65};
int[] dimH = {40,100};
Dimension keyDim = new Dimension(dimW[0],dimH[0]);
Dimension displayDimension = new Dimension(dimW[0],dimH[1]);
Dimension butDim = new Dimension(dimW[1],dimH[0]);
Dimension infoDim = new Dimension(dimW[2],dimH[0]);
JEditorPane display = new JEditorPane();
Font font = new Font("Times new Roman",Font.PLAIN, 14);
JTextField keyIn = new JTextField(24);
JEditorPane msgIn = new JEditorPane();
JScrollPane scrollerD = new JScrollPane(display,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JScrollPane scrollerM = new JScrollPane(msgIn,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
public static void main(String[] args)
{
WindowWin c = new WindowWin();
}
WindowWin(){
super("Test");
// setDesign();
//setSize(380,250);
setSize(460,500);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
GridLayout grid = new GridLayout(4,3);
setLayout(grid);
FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
FlowLayout f2 = new FlowLayout(FlowLayout.CENTER,1,1);
//FlowLayout South
for(int i = 0; i < 4; i++)
row[i] = new JPanel();
row[0].setLayout(f1);
for(int i = 1; i < 4; i++)
row[i].setLayout(f2);
for(int i = 0; i < 4; i++)
{
button[i] = new JButton();
button[i].setText(buttonString[i]);
button[i].setFont(font);
button[i].addActionListener(this);
}
display.setFont(font);
display.setEditable(false);
display.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
display.setPreferredSize(displayDimension);
keyIn.setFont(font);
keyIn.setEditable(true);
keyIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
keyIn.setPreferredSize(keyDim);
msgIn.setFont(font);
msgIn.setEditable(true);
msgIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
msgIn.setPreferredSize(displayDimension);
for(int i = 0; i < 2; i++)
button[i].setPreferredSize(butDim);
for(int i = 2; i < 4; i++)
button[i].setPreferredSize(infoDim);
row[0].add(scrollerD);
add(row[0]);
row[1].add(scrollerM);
add(row[1]);
row[2].add(button[0]);
row[2].add(button[1]);
add(row[2]);
row[3].add(button[2]);
row[3].add(keyIn);
row[3].add(button[3]);
add(row[3]);
setVisible(true);
}
/* public final void setDesign()
{
try
{
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch(Exception e)
{
}
}
*/
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == button[0])
{
}
if(ae.getSource() == button[1])
{
display.setText("Test");
}
}
public void clear()
{
try
{
display.setText("");
}
catch(NullPointerException e)
{
}
}
public void outd()
{
display.setText("");
}
}
答案 0 :(得分:0)
可以使用GridLayout(int rows,int cols,int hgap,int vgap)构造函数调整1行与另一行之间的误差。
https://docs.oracle.com/javase/7/docs/api/java/awt/GridLayout.html有关gridlayout的更多信息,请点击此处。
答案 1 :(得分:0)
这就是GridLayout
的工作原理,每个单元格的空间数量完全相同。相反,请尝试使用GridBagLayout
...
GridBagLayout grid = new GridBagLayout();
setLayout(grid);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
//...
row[0].add(scrollerD);
add(row[0], gbc);
row[1].add(scrollerM);
add(row[1], gbc);
row[2].add(button[0]);
row[2].add(button[1]);
add(row[2], gbc);
row[3].add(button[2]);
row[3].add(keyIn);
row[3].add(button[3]);
add(row[3], gbc);
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class WindowWin extends JFrame implements ActionListener {
JPanel[] row = new JPanel[4];
JButton[] button = new JButton[4];
String[] buttonString = {"Copy to Clipboard", "Go", "Back", "Info"};
int[] dimW = {400, 200, 65};
int[] dimH = {40, 100};
Dimension keyDim = new Dimension(dimW[0], dimH[0]);
Dimension displayDimension = new Dimension(dimW[0], dimH[1]);
Dimension butDim = new Dimension(dimW[1], dimH[0]);
Dimension infoDim = new Dimension(dimW[2], dimH[0]);
JEditorPane display = new JEditorPane();
Font font = new Font("Times new Roman", Font.PLAIN, 14);
JTextField keyIn = new JTextField(24);
JEditorPane msgIn = new JEditorPane();
JScrollPane scrollerD = new JScrollPane(display, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JScrollPane scrollerM = new JScrollPane(msgIn, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
WindowWin c = new WindowWin();
}
});
}
WindowWin() {
super("Test");
// setDesign();
//setSize(380,250);
// setSize(460, 500);
// setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// GridLayout grid = new GridLayout(4, 3);
GridBagLayout grid = new GridBagLayout();
setLayout(grid);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1, 1);
//FlowLayout South
for (int i = 0; i < 4; i++) {
row[i] = new JPanel();
}
row[0].setLayout(f1);
for (int i = 1; i < 4; i++) {
row[i].setLayout(f2);
}
for (int i = 0; i < 4; i++) {
button[i] = new JButton();
button[i].setText(buttonString[i]);
button[i].setFont(font);
button[i].addActionListener(this);
}
display.setFont(font);
display.setEditable(false);
display.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
display.setPreferredSize(displayDimension);
keyIn.setFont(font);
keyIn.setEditable(true);
keyIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
keyIn.setPreferredSize(keyDim);
msgIn.setFont(font);
msgIn.setEditable(true);
msgIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
msgIn.setPreferredSize(displayDimension);
for (int i = 0; i < 2; i++) {
button[i].setPreferredSize(butDim);
}
for (int i = 2; i < 4; i++) {
button[i].setPreferredSize(infoDim);
}
row[0].add(scrollerD);
add(row[0], gbc);
row[1].add(scrollerM);
add(row[1], gbc);
row[2].add(button[0]);
row[2].add(button[1]);
add(row[2], gbc);
row[3].add(button[2]);
row[3].add(keyIn);
row[3].add(button[3]);
add(row[3], gbc);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
/* public final void setDesign()
{
try
{
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch(Exception e)
{
}
}
*/
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == button[0]) {
}
if (ae.getSource() == button[1]) {
display.setText("Test");
}
}
public void clear() {
try {
display.setText("");
} catch (NullPointerException e) {
}
}
public void outd() {
display.setText("");
}
}
你还应该使用pack
而不是setSize
,这将考虑每个外观和平台可能具有的不同框架边框,并使用布局管理器API,它可以更好地完成工作在平台之间渲染差异......
您可能想看看:
...了解更多详情