gpt4 book ai didi

java - 单击 jbutton 打开 JFrame 窗口

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

我在单击时打开新的 jframe 窗口时遇到问题。我认为我做得对,但我不确定。在 Action 监听器中(在下面列出的第一类中)

testing2 test = new testing2();
testing2.setVisible(true);

但是testing2.setVisible(true)说“无法从 JComponent 类型对非静态方法 setVisible(boolean) 进行静态引用”任何帮助表示赞赏。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*************************************************************
* TextPanel Class (with main method)
*************************************************************/

class testing2 extends JPanel {
public testing2() {

JButton btnTesting = new JButton("Testing 2");
add(btnTesting);
}
// override the paintComponent method
// THE MAIN DEMO OF THIS EXAMPLE:

public void paintComponent(Graphics g) {
super.paintComponent(g);
Font f = new Font("SansSerif", Font.BOLD, 14);
Font fi = new Font("SansSerif", Font.BOLD + Font.ITALIC, 14);
FontMetrics fm = g.getFontMetrics(f);
FontMetrics fim = g.getFontMetrics(fi);
int cx = 75; int cy = 100;
g.setFont(f);
g.drawString("Hello, ", cx, cy);
cx += fm.stringWidth("Hello, ");
g.setFont(fi);
g.drawString("World!", cx, cy);
} //paintComponent

//=============================================
///////////// main ////////////////////////////

public static void main(String[] args) {
JFrame f = new MyFrame("My Hello World Frame");
f.show();
} //main

} //class TextPanel

/*************************************************************
MyFrame Class
*************************************************************/

class MyFrame extends JFrame {
public MyFrame(String s) {
// Frame Parameters
setTitle(s);
setSize(300,200); // default size is 0,0
setLocation(10,200); // default is 0,0 (top left corner)

// Window Listeners
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} //windowClosing
}); //addWindowLister

// Add Panels
Container contentPane = getContentPane();
contentPane.add(new testing());

} //constructor MyFrame
} //class MyFrame
<小时/>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*************************************************************
* TextPanel Class (with main method)
*************************************************************/

class testing extends JPanel {
public testing() {

JButton btnTesting = new JButton("Testing 2");
btnTesting.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});
add(btnTesting);
}
// override the paintComponent method
// THE MAIN DEMO OF THIS EXAMPLE:

public void paintComponent(Graphics g) {
super.paintComponent(g);
Font f = new Font("SansSerif", Font.BOLD, 14);
Font fi = new Font("SansSerif", Font.BOLD + Font.ITALIC, 14);
FontMetrics fm = g.getFontMetrics(f);
FontMetrics fim = g.getFontMetrics(fi);
int cx = 75; int cy = 100;
g.setFont(f);
g.drawString("Hello, ", cx, cy);
cx += fm.stringWidth("Hello, ");
g.setFont(fi);
g.drawString("World!", cx, cy);
} //paintComponent

//=============================================
///////////// main ////////////////////////////

public static void main(String[] args) {
JFrame f = new MyFrame("My Hello World Frame");
f.show();
} //main

} //class TextPanel

/*************************************************************
MyFrame Class
*************************************************************/

class MyFrame extends JFrame {
public MyFrame(String s) {
// Frame Parameters
setTitle(s);
setSize(300,200); // default size is 0,0
setLocation(10,200); // default is 0,0 (top left corner)

System.err.println("Here");
testing2 test = new testing2();
test.setVisible(true);


// Window Listeners
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} //windowClosing
}); //addWindowLister

// Add Panels
Container contentPane = getContentPane();
contentPane.add(new testing());

} //constructor MyFrame
} //class MyFrame

最佳答案

使用测试,而不是测试2

            test.setVisible(true); //was testing2.setVisible(true);

关于java - 单击 jbutton 打开 JFrame 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31339852/

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