我已经阅读了很多关于Swing的内容,但无法弄清楚为什么我的GUI不能显示?它编译得非常好。我猜它是因为我没有正确使用setVisible()方法,或者我没有使用pack()方法。我必须强调,我非常喜欢Swing。我不是在寻找有人为我做这件事,只是指向正确的方向。
我的代码如下:
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class TestGUI3 implements ActionListener
{
JRadioButton rb1, rb2, rb3;
JFrame f;
JButton b;
public void TestGUI3(NewGameGUI NewGameGUIMethod)
{
f = new JFrame("Welcome to Ballon d'or");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rb1 = new JRadioButton("Start New Game");
rb2 = new JRadioButton("Load Game");
rb3 = new JRadioButton("Quit");
rb1.setBounds(50,100,170,30);
rb2.setBounds(50,150,170,30);
rb3.setBounds(50,200,170,30);
ButtonGroup bg=new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
b = new JButton("OK");
b.setBounds(200,250,180,30);
b.addActionListener(this);
f.add(rb1);
f.add(rb2);
f.add(rb3);
f.add(b);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
@Override public void actionPerformed(ActionEvent e)
{
if(rb1.isSelected())
{
f.dispose();
NewGameGUI test = new NewGameGUI();
test.getFrame().setVisible(true);
//JOptionPane.showMessageDialog(f,"New Game");
}
if(rb2.isSelected())
{
JOptionPane.showMessageDialog(f,"Load Game");
}
if(rb3.isSelected())
{
System.exit(0);
}
}
public static void main(String[] args)
{
new TestGUI3();
}
}
//我的其他班级
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class NewGameGUI implements ActionListener
{
JRadioButton rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8 , rb9 , rb10, rb11, rb12, rb13, rb14, rb15, rb16, rb17, rb18, rb19, rb20;
JFrame f2;
JButton b;
JLabel label1;
public void NewGameGUIMethod()
{
label1 = new JLabel("Please choose a team");
f2 = new JFrame("Ballon d'or");
f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rb1 = new JRadioButton("Arsenal");
rb2 = new JRadioButton("Aston Villa");
rb3 = new JRadioButton("Bournemouth");
rb4 = new JRadioButton("Chelsea");
rb5 = new JRadioButton("Crystal Palace");
rb6 = new JRadioButton("Everton");
rb7 = new JRadioButton("Leicester City");
rb8 = new JRadioButton("Liverpool");
rb9 = new JRadioButton("Manchester United");
rb10 = new JRadioButton("Manchester City");
rb11 = new JRadioButton("Newcastle United");
rb12 = new JRadioButton("Norwich City");
rb13 = new JRadioButton("Southampton");
rb14 = new JRadioButton("Stoke City");
rb15 = new JRadioButton("Sunderland");
rb16 = new JRadioButton("Swansea City");
rb17 = new JRadioButton("Tottenham Hotspur");
rb18 = new JRadioButton("Watford");
rb19 = new JRadioButton("West Brom");
rb20 = new JRadioButton("West Ham");
label1.setBounds(50,25,200,30);
rb1.setBounds(50,100,170,30);
rb2.setBounds(50,150,170,30);
rb3.setBounds(50,200,170,30);
ButtonGroup bg=new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
bg.add(rb4);
bg.add(rb5);
bg.add(rb6);
bg.add(rb7);
bg.add(rb8);
bg.add(rb9);
bg.add(rb10);
bg.add(rb11);
bg.add(rb12);
bg.add(rb13);
bg.add(rb14);
bg.add(rb15);
bg.add(rb16);
bg.add(rb17);
bg.add(rb18);
bg.add(rb19);
bg.add(rb20);
b = new JButton("OK");
b.setBounds(200,250,180,30);
b.addActionListener(this);
f2.add(rb1);
f2.add(rb2);
f2.add(rb3);
f2.add(rb4);
f2.add(rb5);
f2.add(rb6);
f2.add(rb7);
f2.add(rb8);
f2.add(rb9);
f2.add(rb10);
f2.add(rb11);
f2.add(rb12);
f2.add(rb13);
f2.add(rb14);
f2.add(rb15);
f2.add(rb16);
f2.add(rb17);
f2.add(rb18);
f2.add(rb19);
f2.add(rb20);
f2.add(b);
f2.setSize(500,500);
f2.setLayout(null);
f2.setVisible(true);
NewGameGUI test = new NewGameGUI();
test.setFrame(f2);
}
public void setFrame(JFrame f2){
this.f2 = f2;
}
public JFrame getFrame(){
return f2;
}
public void actionPerformed(ActionEvent e)
{
if(rb1.isSelected())
{
f2.dispose();
//JOptionPane.showMessageDialog(f,"New Game");
}
if(rb2.isSelected())
{
JOptionPane.showMessageDialog(f2,"Load Game");
}
if(rb3.isSelected())
{
System.exit(0);
}
}
public static void main(String[] args)
{
new TestGUI3();
}
}
答案 0 :(得分:4)
你的两个文件都有这个
public static void main(String[] args)
{
new TestGUI3();
}
这将调用TestGUI3
类的默认构造函数,它不执行任何操作。
此外,您似乎对构造函数与实例方法的理解存在冲突。
public void TestGUI3(NewGameGUI NewGameGUIMethod)
和
public void NewGameGUIMethod()
构造函数没有返回类型。另外,其中第二个甚至不是它所在类的名称......
我建议您执行类似class TestGUI3 extends JFrame implements ActionListener
的操作,这样,您就不需要对框架进行内联引用,而是您的类 框架你可以setVisible()
来展示它们。
答案 1 :(得分:0)
1 - 删除void因为添加它会使它成为普通方法而不是构造函数
public void NewGameGUIMethod()
,也应该命名为NewGameGUI,最终名称为public NewGameGUI()
2-取消其他类中的void,对于上述相同
public void TestGUI3(NewGameGUI NewGameGUIMethod)
,也不需要将NewGameGUIMethod
作为参数传递,因为TestGUI3
中未使用NewGameGUIMethod()
3-每次调用 //NewGameGUI test = new NewGameGUI();
//test.setFrame(f2);
f2.setVisible(true);
时都会构造一个新帧,这会引起递归,只是注释帧的创建,并将构造的实例设置为可见,如下所示:
public static void main(String[] args) {
new NewGameGUI();
}
4-将NewGameGUI的主要方法更新为:
NewGameGUI
package test.q5;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class NewGameGUI implements ActionListener {
JRadioButton rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8, rb9, rb10, rb11, rb12, rb13, rb14, rb15, rb16, rb17, rb18, rb19, rb20;
JFrame f2;
JButton b;
JLabel label1;
public NewGameGUI() {
label1 = new JLabel("Please choose a team");
f2 = new JFrame("Ballon d'or");
f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rb1 = new JRadioButton("Arsenal");
rb2 = new JRadioButton("Aston Villa");
rb3 = new JRadioButton("Bournemouth");
rb4 = new JRadioButton("Chelsea");
rb5 = new JRadioButton("Crystal Palace");
rb6 = new JRadioButton("Everton");
rb7 = new JRadioButton("Leicester City");
rb8 = new JRadioButton("Liverpool");
rb9 = new JRadioButton("Manchester United");
rb10 = new JRadioButton("Manchester City");
rb11 = new JRadioButton("Newcastle United");
rb12 = new JRadioButton("Norwich City");
rb13 = new JRadioButton("Southampton");
rb14 = new JRadioButton("Stoke City");
rb15 = new JRadioButton("Sunderland");
rb16 = new JRadioButton("Swansea City");
rb17 = new JRadioButton("Tottenham Hotspur");
rb18 = new JRadioButton("Watford");
rb19 = new JRadioButton("West Brom");
rb20 = new JRadioButton("West Ham");
label1.setBounds(50, 25, 200, 30);
rb1.setBounds(50, 100, 170, 30);
rb2.setBounds(50, 150, 170, 30);
rb3.setBounds(50, 200, 170, 30);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
bg.add(rb4);
bg.add(rb5);
bg.add(rb6);
bg.add(rb7);
bg.add(rb8);
bg.add(rb9);
bg.add(rb10);
bg.add(rb11);
bg.add(rb12);
bg.add(rb13);
bg.add(rb14);
bg.add(rb15);
bg.add(rb16);
bg.add(rb17);
bg.add(rb18);
bg.add(rb19);
bg.add(rb20);
b = new JButton("OK");
b.setBounds(200, 250, 180, 30);
b.addActionListener(this);
f2.add(rb1);
f2.add(rb2);
f2.add(rb3);
f2.add(rb4);
f2.add(rb5);
f2.add(rb6);
f2.add(rb7);
f2.add(rb8);
f2.add(rb9);
f2.add(rb10);
f2.add(rb11);
f2.add(rb12);
f2.add(rb13);
f2.add(rb14);
f2.add(rb15);
f2.add(rb16);
f2.add(rb17);
f2.add(rb18);
f2.add(rb19);
f2.add(rb20);
f2.add(b);
f2.setSize(500, 500);
f2.setLayout(null);
f2.setVisible(true);
//NewGameGUI test = new NewGameGUI();
//test.setFrame(f2);
f2.setVisible(true);
}
public void setFrame(JFrame f2) {
this.f2 = f2;
}
public JFrame getFrame() {
return f2;
}
public void actionPerformed(ActionEvent e) {
if (rb1.isSelected()) {
f2.dispose();
// JOptionPane.showMessageDialog(f,"New Game");
}
if (rb2.isSelected()) {
JOptionPane.showMessageDialog(f2, "Load Game");
}
if (rb3.isSelected()) {
System.exit(0);
}
}
public static void main(String[] args) {
new NewGameGUI();
}
}
类的完整类代码:
package test.q5;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class TestGUI3 implements ActionListener {
JRadioButton rb1, rb2, rb3;
JFrame f;
JButton b;
public TestGUI3() {
f = new JFrame("Welcome to Ballon d'or");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rb1 = new JRadioButton("Start New Game");
rb2 = new JRadioButton("Load Game");
rb3 = new JRadioButton("Quit");
rb1.setBounds(50, 100, 170, 30);
rb2.setBounds(50, 150, 170, 30);
rb3.setBounds(50, 200, 170, 30);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
b = new JButton("OK");
b.setBounds(200, 250, 180, 30);
b.addActionListener(this);
f.add(rb1);
f.add(rb2);
f.add(rb3);
f.add(b);
f.setSize(500, 500);
f.setLayout(null);
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (rb1.isSelected()) {
f.dispose();
NewGameGUI test = new NewGameGUI();
test.getFrame().setVisible(true);
// JOptionPane.showMessageDialog(f,"New Game");
}
if (rb2.isSelected()) {
JOptionPane.showMessageDialog(f, "Load Game");
}
if (rb3.isSelected()) {
System.exit(0);
}
}
public static void main(String[] args) {
new TestGUI3();
}
}
第2课:
$http({
method: "POST",
url: 'http://sysdatacenter.com/WS/DataService.svc/Customer',
data: '[{"AddressArabic":"fgdfgdfg","AddressEnglish":"fw4534sdv","BusinessCategoryID":3,"CityID":3,"Comments":null,"CompanyName":"rwerwer","CompanyNameArabic":"ssdfgdfgd","ContactNo":3534535,"ContactPerson":"sdfsdfsd","CreatedBy":"admin","CreatedDate":"06-10-2016 12:19:25","CustomerCode":"werw","CustomerID":1,"DeviceTaskID":null,"Email":"rwerwrw@sfsdf.dgd","Fax":3453453,"Flexfld1":null,"Flexfld2":null,"Flexfld3":null,"Flexfld4":null,"Flexfld5":null,"IsActive":1,"IsSync":1,"ModifiedBy":"admin","ModifiedDate":"06-10-2016 12:19:25","POBox":345,"TradeLicenseNo":"erwe","TradeLicenseNoArabic":"gdfgdfgd","TransactionType":"I"},{"AddressArabic":"fgdfgdfg","AddressEnglish":"fw4534sdv","BusinessCategoryID":3,"CityID":3,"Comments":null,"CompanyName":"rwerwer","CompanyNameArabic":"ssdfgdfgd","ContactNo":3534535,"ContactPerson":"sdfsdfsd","CreatedBy":"admin","CreatedDate":"06-10-2016 12:19:25","CustomerCode":"werw","CustomerID":1,"DeviceTaskID":null,"Email":"rwerwrw@sfsdf.dgd","Fax":3453453,"Flexfld1":null,"Flexfld2":null,"Flexfld3":null,"Flexfld4":null,"Flexfld5":null,"IsActive":1,"IsSync":1,"ModifiedBy":"admin","ModifiedDate":"06-10-2016 12:19:25","POBox":345,"TradeLicenseNo":"erwe","TradeLicenseNoArabic":"gdfgdfgd","TransactionType":"I"}]',
headers: {"deviceid":"ABC1234"}
}).then(function (data, status, headers, config) {
console.log(r)
}, function (err) {
});
最后注意事项:您应该考虑阅读有关摆动布局(FlowLayout,BorderLayout,GridLayout,BoxLayout)的内容,以便能够正确控制您的组件。