我在点击时打开新的jframe窗口时出现问题。 我认为我做得对,但我不确定。 在动作监听器中(在下面列出的第一类中)
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
答案 0 :(得分:1)
使用测试,而不是测试2
test.setVisible(true); //was testing2.setVisible(true);