gpt4 book ai didi

Java - Jframe 不显示按钮标签或文本字段

转载 作者:行者123 更新时间:2023-12-01 22:24:56 24 4
gpt4 key购买 nike

我是java新手,但是当我尝试创建一个新框架时,我得到的只是一个打开的带有白色背景的新窗口,并且没有添加任何按钮、文本框或标签。我不知道我做错了什么?

private static void MainGui(){

MainInterfaces MainGui = new MainInterfaces();

MainGui.setTitle(name+ "'s Inbox");
MainGui.setSize(600,600);
MainGui.setVisible(true);
MainGui.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

主接口(interface)类

class MainInterfaces extends JFrame implements ActionListener
{

private JButton send;
private JTextField to, subject, message;
private JLabel toLbl, subjectLbl, messageLbl;


public MainInterfaces() {

setLayout(new FlowLayout());
toLbl = new JLabel("Recipient: ");
subjectLbl = new JLabel("Subject: ");
messageLbl = new JLabel("Message: ");

to = new JTextField(32);
subject = new JTextField(32);
message = new JTextField(32);

send = new JButton("SEND");

add(toLbl);
add(to);
add(subjectLbl);
add(subject);
add(messageLbl);
add(message);
add(send);

}

public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub

}
}

最佳答案

您的代码无法编译,因为您有很多错误(您没有 main 方法private static void MainGui(){ 没有任何意义等) 。请尝试使用此代码,它可以很好地编译并打开 GUI,显示您想要的所有内容(按钮、标签等):

GUI Working

1) 类MainGui.java

class MainGui{
public static void main(String[] args) {

MainInterfaces MainGui = new MainInterfaces();

MainGui.setTitle("'s Inbox");
MainGui.setSize(600,600);
MainGui.setVisible(true);
MainGui.setDefaultCloseOperation(MainGui.EXIT_ON_CLOSE);
}
}

2)类MainInterfaces.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MainInterfaces extends JFrame implements ActionListener
{

private JButton send;
private JTextField to, subject, message;
private JLabel toLbl, subjectLbl, messageLbl;


public MainInterfaces() {



setLayout(new FlowLayout());
toLbl = new JLabel("Recipient: ");
subjectLbl = new JLabel("Subject: ");
messageLbl = new JLabel("Message: ");

to = new JTextField(32);
subject = new JTextField(32);
message = new JTextField(32);

send = new JButton("SEND");

add(toLbl);
add(to);
add(subjectLbl);
add(subject);
add(messageLbl);
add(message);
add(send);




}

public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub

}
}

关于Java - Jframe 不显示按钮标签或文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28971701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com