Java 为什么我的程序窗口显示不正确?
该项目是关于设计一个GUI调查问卷,其中人们选择某些选项,然后与开发人员(即我的合作伙伴和我)进行比较 它在早些时候工作,但我的JRadioButton相关代码坚决拒绝正常工作。我看不到任何按钮,只显示标签。我能做些什么来解决这个问题?这是我的密码:Java 为什么我的程序窗口显示不正确?,java,swing,awt,actionlistener,Java,Swing,Awt,Actionlistener,该项目是关于设计一个GUI调查问卷,其中人们选择某些选项,然后与开发人员(即我的合作伙伴和我)进行比较 它在早些时候工作,但我的JRadioButton相关代码坚决拒绝正常工作。我看不到任何按钮,只显示标签。我能做些什么来解决这个问题?这是我的密码: import java.awt.*; import java.awt.event.ActionListener; import javax.swing.*; import java.awt.Container; import javax.swing
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.Container;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class MyFirstGUIDriver extends JFrame{
public static void main(String[] args) {
MyFirstGUI first=new MyFirstGUI();
first.setTitle("MyFirstGUI");
first.setSize(700,800);
first.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
first.setVisible(true);
}
}
和定义类:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Container;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
@SuppressWarnings("Serial")
public class MyFirstGUI extends JFrame implements ActionListener {
JPanel apanel;
JButton quitButton;
JButton submitButton;
JLabel label1;
JLabel label2;
JLabel label3;
JLabel label4;
JLabel label5;
JLabel label6;
JTextField Text1;
JTextField Text2;
JTextField Text3;
JTextField Text4;
JTextField Text5;
JTextField Text6;
JFrame frame=new JFrame();
public MyFirstGUI(){
apanel=new JPanel(null);
quitButton=new JButton(" Quit ");
quitButton.addActionListener(this);
quitButton.setBounds(10,10,80,20);
submitButton=new JButton("Submit");
submitButton.setBounds(10,390,80,20);
submitButton.addActionListener(this);
label1 = new JLabel("Enter your name please.");
label1.setBounds(10,30,150,20);
Text1=new JTextField(10);
Text1.setBounds(10,60,150,20);
label2 = new JLabel("Enter your age please.");
label2.setBounds(10,90,150,20);
Text2=new JTextField(10);
Text2.setBounds(10,120,150,20);
label3 = new JLabel("Enter your favorite color please.");
label3.setBounds(10,150,180,20);
Text3=new JTextField(10);
Text3.setBounds(10,180,150,20);
label4 = new JLabel("PC or Console?");
label4.setBounds(10,210,150,20);
Text4=new JTextField(10);
Text4.setBounds(10,240,150,20);
label5 = new JLabel("Coke of Pepsi?");
label5.setBounds(10,270,150,20);
Text5=new JTextField(10);
Text5.setBounds(10,300,150,20);
label6 = new JLabel("McDonalds or Burger King?");
label6.setBounds(10,330,170,20);
Text6=new JTextField(10);
Text6.setBounds(10,360,150,20);
apanel.add(quitButton);
apanel.add(submitButton);
apanel.add(label1);
apanel.add(label2);
apanel.add(label3);
apanel.add(label4);
apanel.add(label5);
apanel.add(label6);
apanel.add(Text1);
apanel.add(Text2);
apanel.add(Text3);
apanel.add(Text4);
apanel.add(Text5);
apanel.add(Text6);
this.add(apanel);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(0, 1));
JRadioButton e1=new JRadioButton("PC");
JRadioButton e2=new JRadioButton("Console");
JRadioButton e3=new JRadioButton("Burger King");
JRadioButton e4=new JRadioButton("McDonalds");
JRadioButton e5=new JRadioButton("Coke");
JRadioButton e6=new JRadioButton("Pepsi");
ButtonGroup bg1 = new ButtonGroup( );
ButtonGroup bg2 = new ButtonGroup( );
ButtonGroup bg3 = new ButtonGroup( );
bg1.add(e1);
bg1.add(e2);
bg2.add(e3);
bg2.add(e4);
bg3.add(e5);
bg3.add(e6);
contentPane.add(e1);
contentPane.add(e2);
contentPane.add(e3);
contentPane.add(e4);
contentPane.add(e5);
contentPane.add(e6);
apanel.add(e1);
apanel.add(e2);
apanel.add(e3);
apanel.add(e4);
apanel.add(e5);
apanel.add(e6);
}
public void actionPerformed(ActionEvent event){
if(event.getSource()==quitButton){
System.exit(0);
}
if(event.getSource()==submitButton){
String a1="Steven";
int a2=14;
String a3="Green";
String a4="PC";
String a5="Coke";
String a6="McDonalds";
String b1=Text1.getText();
String c2=Text2.getText();
int b2= Integer.parseInt(c2);
String b3=Text3.getText();
String b4=Text4.getText();
String b5=Text5.getText();
String b6=Text6.getText();
String d1="";
if(a2>b2){d1="You are younger than me.";}
else if(b2>a2){d1="You are older than me.";}
else{d1="You are the same age as me.";};
JOptionPane.showMessageDialog(frame,"Hello "+b1+". My name is "+a1+"."+d1);
}
}
}
注意:我的程序无论如何都不完整。我仍然需要使按钮正常工作,并替换我想要改进的文本框代码(确实有效)。您需要设置单选按钮的背景,请尝试以下操作并根据需要对齐
e1.setBounds(200, 260, 130, 23);
e2.setBounds(300, 260, 130, 23);
e3.setBounds(400, 260, 130, 23);
e4.setBounds(500, 260, 130, 23);
e5.setBounds(600, 260, 130, 23);
e6.setBounds(700, 260, 130, 23);
MyFirstGUIDriver
不需要扩展JFrame
您的代码太乱了(对不起)MyFirstGUI
扩展了JFrame
,但是您在其中创建了另一个JFrame
,对于这个(内部)实例,您似乎添加了单选按钮,但是这个内部框架从来都不可见。您不应该从JFrame
扩展,而应该从JPanel
扩展,并将内部JFrame
一起删除。您应该使用“驱动程序”创建JFrame
的实例,然后将gui添加到该实例中最小化您的代码,并指出在什么时候它不再工作,“null”将始终困扰您,不要使用它们。您也决不会将contentPane
添加到您的框架中。Idk先生,你应该开始让你的代码可读。Java GUI必须在不同的操作系统上工作,屏幕大小,屏幕分辨率等等。在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。