我正在使用一个简单的GUI,我的文本字段很难。
这就是我正在使用的。右边的那个是它看起来的样子,左边的是我做的。我已经成功地将标签地址1,地址2等等。但是当我开始放置文本字段时,它就消失了。我尝试使用setSize,setLocation,但没有任何作用。
同样的事情发生在上层小组“Paymen Method”,正如预期结果所示,应该有一个文本字段。我在我的代码中有它们但是它们在运行时没有显示出来。请帮助。
这是我的代码:
import javax.swing.*;
import java.awt.*;
public class PanelDemo extends javax.swing.JFrame{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 350;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 250;
public static void main(String[] args){
//Frame
JFrame contentPane = new javax.swing.JFrame();
contentPane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane.setSize(300,350);
contentPane.setResizable(false);
contentPane.setLayout(new BorderLayout());
//Payment Panel
JPanel paymentPanel = new javax.swing.JPanel();
paymentPanel.setLayout(new BorderLayout());
//paymentPanel.setPreferredSize(new java.awt.Dimension(270, 90));
paymentPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Payment Method", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12)));
////Components inside Payment Panel
////A) Panels: Radio and Details
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(3,1));
JRadioButton Rbutton1 = new JRadioButton("Credit Card");
JRadioButton Rbutton2 = new JRadioButton("E-Funds");
JRadioButton Rbutton3 = new JRadioButton("Check");
Rbutton3.setSelected(true);
ButtonGroup Bgroup = new ButtonGroup();
Bgroup.add(Rbutton1);
Bgroup.add(Rbutton2);
Bgroup.add(Rbutton3);
radioPanel.add(Rbutton1);
radioPanel.add(Rbutton2);
radioPanel.add(Rbutton3);
//I thought of using a panel as a gap, but still didn't work
/*JPanel gap = new JPanel();
gap.setLayout(new BorderLayout());
gap.setPreferredSize(new java.awt.Dimension(10, 90));*/
JPanel detailsPanel = new JPanel();
detailsPanel.setLayout(new GridLayout(2,1));
//detailsPanel
JLabel Accountnum = new JLabel("Account number:");
JTextField Account = new JTextField();
Account.setPreferredSize(new java.awt.Dimension(90, 40));
detailsPanel.add(Accountnum);
detailsPanel.add(Account);
paymentPanel.add(gap, BorderLayout.CENTER);
paymentPanel.add(detailsPanel);
paymentPanel.add(radioPanel);
contentPane.add(paymentPanel, BorderLayout.PAGE_START);
////Address Information Panel
JPanel addressPanel = new JPanel();
addressPanel.setLayout(new BorderLayout());
addressPanel.setSize(new java.awt.Dimension(270, 80));
addressPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Adress Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12)));
contentPane.add(addressPanel, BorderLayout.CENTER);
////Components inside the Address Information Panel
////A) Labels (Address 1, Address 2,...)
JPanel InfoLabel = new JPanel();
InfoLabel.setLayout(new GridLayout(5,1));
JLabel address1 = new JLabel("Address 1:");
JLabel address2 = new JLabel("Address 2:");
JLabel city = new JLabel("City:");
JLabel state = new JLabel("State:");
JLabel zip = new JLabel("Zip Code:");
InfoLabel.add(address1);
InfoLabel.add(address2);
InfoLabel.add(city);
InfoLabel.add(state);
InfoLabel.add(zip);
////B)Text Fields
JPanel infotext = new JPanel();
infotext.setLayout(new GridLayout(5,1));
JTextField text1 = new JTextField();
JTextField text2 = new JTextField();
JTextField text3 = new JTextField();
JTextField text4 = new JTextField();
JTextField text5 = new JTextField();
addressPanel.add(InfoLabel);
addressPanel.add(infotext);
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
JLabel test = new JLabel("test");
controlPanel.add(test);
contentPane.add(controlPanel, BorderLayout.PAGE_END);
contentPane.setVisible(true);
}
}
答案 0 :(得分:3)
我稍微修改了你的代码,以获得满足你需要的版本。基本上,我所做的只是清理你使用布局管理器的方式。如果您希望必须执行多个此类用户界面,那么您可以尝试阅读Swing布局管理器的javadoc,以便更好地理解它们。
以下是工作版本:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class PanelDemo extends javax.swing.JFrame {
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 350;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 250;
public static void main(String[] args) {
// Frame
JFrame contentPane = new javax.swing.JFrame();
contentPane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane.setSize(300, 350);
contentPane.setResizable(false);
contentPane.setLayout(new BorderLayout());
// Payment Panel
JPanel paymentPanel = new javax.swing.JPanel();
paymentPanel.setLayout(new BorderLayout());
// paymentPanel.setPreferredSize(new java.awt.Dimension(270, 90));
paymentPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Payment Method",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12)));
// //Components inside Payment Panel
// //A) Panels: Radio and Details
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(3, 1));
JRadioButton Rbutton1 = new JRadioButton("Credit Card");
JRadioButton Rbutton2 = new JRadioButton("E-Funds");
JRadioButton Rbutton3 = new JRadioButton("Check");
Rbutton3.setSelected(true);
ButtonGroup Bgroup = new ButtonGroup();
Bgroup.add(Rbutton1);
Bgroup.add(Rbutton2);
Bgroup.add(Rbutton3);
radioPanel.add(Rbutton1);
radioPanel.add(Rbutton2);
radioPanel.add(Rbutton3);
// I thought of using a panel as a gap, but still didn't work
/*
* JPanel gap = new JPanel(); gap.setLayout(new BorderLayout());
* gap.setPreferredSize(new java.awt.Dimension(10, 90));
*/
JPanel detailsPanel = new JPanel();
detailsPanel.setLayout(new GridLayout(2, 2));
// detailsPanel
JLabel Accountnum = new JLabel("Account number:");
JTextField Account = new JTextField();
Account.setPreferredSize(new java.awt.Dimension(90, 40));
detailsPanel.add(Accountnum);
detailsPanel.add(Account);
// paymentPanel.add(gap, BorderLayout.CENTER);
paymentPanel.add(radioPanel, BorderLayout.CENTER);
paymentPanel.add(detailsPanel, BorderLayout.EAST);
contentPane.add(paymentPanel, BorderLayout.PAGE_START);
// //Address Information Panel
JPanel addressPanel = new JPanel();
addressPanel.setLayout(new BorderLayout());
addressPanel.setSize(new java.awt.Dimension(270, 80));
addressPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Adress Information",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12)));
contentPane.add(addressPanel, BorderLayout.CENTER);
// //Components inside the Address Information Panel
// //A) Labels (Address 1, Address 2,...)
JPanel InfoLabel = new JPanel();
InfoLabel.setLayout(new GridLayout(5, 2));
JLabel address1 = new JLabel("Address 1:");
JLabel address2 = new JLabel("Address 2:");
JLabel city = new JLabel("City:");
JLabel state = new JLabel("State:");
JLabel zip = new JLabel("Zip Code:");
JTextField text1 = new JTextField();
JTextField text2 = new JTextField();
JTextField text3 = new JTextField();
JTextField text4 = new JTextField();
JTextField text5 = new JTextField();
InfoLabel.add(address1);
InfoLabel.add(text1);
InfoLabel.add(address2);
InfoLabel.add(text2);
InfoLabel.add(city);
InfoLabel.add(text3);
InfoLabel.add(state);
InfoLabel.add(text4);
InfoLabel.add(zip);
InfoLabel.add(text5);
// //B)Text Fields
JPanel infotext = new JPanel();
infotext.setLayout(new GridLayout(5, 1));
addressPanel.add(InfoLabel, BorderLayout.CENTER);
// addressPanel.add(infotext);
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
JLabel test = new JLabel("test");
controlPanel.add(test);
contentPane.add(controlPanel, BorderLayout.PAGE_END);
contentPane.setVisible(true);
}
}
答案 1 :(得分:2)
您忘了设置布局区域。
paymentPanel.add(detailsPanel, BorderLayout.EAST);
paymentPanel.add(radioPanel, BorderLayout.CENTER);
addressPanel.add(InfoLabel,BorderLayout.WEST);
addressPanel.add(infotext, BorderLayout.CENTER);