JPanel 没有显示
JPanel is not showing up
我直接从书中复制了例子。该代码应该在 JFrame 上绘制一些东西,但没有显示(JFrame 除外)这是 class 和主要方法
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class JavaApplication24 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame frame = new JFrame("Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(480,270);
frame.setVisible(true);
NewClass panel = new NewClass();
frame.add(BorderLayout.CENTER, panel);
}
这是 JPanel
的子class
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
public class NewClass extends JPanel {
@Override
public void paintComponent(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0,0,100,100);
g.setColor(Color.GREEN);
g.drawRect(50,50,100,100);
g.setColor(Color.RED);
g.drawString("Hello",200,200);
g.setColor(Color.RED);
g.fillOval(240,40,100,30);
}
}
第 1 期
您的 NewClass
应该提供布局管理器(BorderLayout
在这种情况下)可以决定如何最好地布局您的组件的大小提示。
您还应该在进行任何自定义绘画之前调用 super.paintComponent
,否则您最终会出现无休止的渲染瑕疵
public class NewClass extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(0, 0, 100, 100);
g.setColor(Color.GREEN);
g.drawRect(50, 50, 100, 100);
g.setColor(Color.RED);
g.drawString("Hello", 200, 200);
g.setColor(Color.RED);
g.fillOval(240, 40, 100, 30);
}
}
I copied the examples straight from the book
我希望你只是犯了一些小错误,否则我会担心这本书的有效性:P
您还应该从事件调度线程的上下文中启动您的 UI,这往往可以解决可能出现在不同平台上的问题...
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new NewClass());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
有关详细信息,请参阅 Initial Threads
第 2 期
你不应该让 UI 可见,直到你建立它,而不是
frame.setVisible(true);
NewClass panel = new NewClass();
frame.add(BorderLayout.CENTER, panel);
使用更像...
NewClass panel = new NewClass();
frame.add(BorderLayout.CENTER, panel);
frame.setVisible(true);
您可以通过多种方式在框架可见后触发和更新,但这只是最简单的修复方法
我直接从书中复制了例子。该代码应该在 JFrame 上绘制一些东西,但没有显示(JFrame 除外)这是 class 和主要方法
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class JavaApplication24 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame frame = new JFrame("Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(480,270);
frame.setVisible(true);
NewClass panel = new NewClass();
frame.add(BorderLayout.CENTER, panel);
}
这是 JPanel
的子classimport java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
public class NewClass extends JPanel {
@Override
public void paintComponent(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0,0,100,100);
g.setColor(Color.GREEN);
g.drawRect(50,50,100,100);
g.setColor(Color.RED);
g.drawString("Hello",200,200);
g.setColor(Color.RED);
g.fillOval(240,40,100,30);
}
}
第 1 期
您的 NewClass
应该提供布局管理器(BorderLayout
在这种情况下)可以决定如何最好地布局您的组件的大小提示。
您还应该在进行任何自定义绘画之前调用 super.paintComponent
,否则您最终会出现无休止的渲染瑕疵
public class NewClass extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(0, 0, 100, 100);
g.setColor(Color.GREEN);
g.drawRect(50, 50, 100, 100);
g.setColor(Color.RED);
g.drawString("Hello", 200, 200);
g.setColor(Color.RED);
g.fillOval(240, 40, 100, 30);
}
}
I copied the examples straight from the book
我希望你只是犯了一些小错误,否则我会担心这本书的有效性:P
您还应该从事件调度线程的上下文中启动您的 UI,这往往可以解决可能出现在不同平台上的问题...
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new NewClass());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
有关详细信息,请参阅 Initial Threads
第 2 期
你不应该让 UI 可见,直到你建立它,而不是
frame.setVisible(true);
NewClass panel = new NewClass();
frame.add(BorderLayout.CENTER, panel);
使用更像...
NewClass panel = new NewClass();
frame.add(BorderLayout.CENTER, panel);
frame.setVisible(true);
您可以通过多种方式在框架可见后触发和更新,但这只是最简单的修复方法