我正在使用一个简单的 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);
}
}