2

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

}
4

3 回答 3

4

我稍微修改了您的代码以达到您需要的版本。基本上,我所做的只是清理您使用布局管理器的方式。如果您希望您必须做几个这样的用户界面,那么您可以尝试阅读 Swing 布局管理器的 javadocs 以便更好地理解它们。

这是工作版本:

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);
  }
}
于 2012-09-04T12:02:02.920 回答
2

您忘记为布局设置区域。

paymentPanel.add(detailsPanel, BorderLayout.EAST);
paymentPanel.add(radioPanel, BorderLayout.CENTER);

addressPanel.add(InfoLabel,BorderLayout.WEST); 
addressPanel.add(infotext, BorderLayout.CENTER);
于 2012-09-04T12:20:37.137 回答
0

您的代码应调整为:

import javax.swing.*;
import java.awt.*;
public class App 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 GridLayout(1,2));
      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);

      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); // THIS PRODUCES SYNTAX ERROR
      paymentPanel.add(radioPanel);
      paymentPanel.add(detailsPanel);
      contentPane.add(paymentPanel, BorderLayout.PAGE_START);

////Address Information Panel
      JPanel addressPanel = new JPanel();
      addressPanel.setLayout(new GridLayout(1,2));
      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();

      infotext.add(text1);
      infotext.add(text2);
      infotext.add(text3);
      infotext.add(text4);
      infotext.add(text5);

      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);
   }

}

即,

  • 您必须删除该paymentPanel.add(gap, BorderLayout.CENTER);行,因为它会产生语法错误
  • 您必须指定paymentPanel,addressPanel才能拥有,GridLayout(1,2)以便它们能够以不重叠的方式包含radioPaneldetailsPanel和面板InfoLabelinfotext
  • 您必须radioPanelpaymentPanel _detailsPanel
  • 您必须实际将文本字段添加到infotext面板,即infotext.add(text1);等。

注意:我知道现在你可能不需要这个,但我想写这个答案,因为我不太喜欢@Dan 的答案,例如,他保留了BorderLayout并且paymentpanel基本上写了他自己的版本代码,而不是修改您的代码,使其能够正常工作。反正。

于 2022-01-19T17:32:35.203 回答