在一个 JPanel 中包含的多个 JPanel 中绘图
Drawing in multiple JPanels contained in one JPanel
我有一个 class,它扩展了包含 3 个其他面板的 JPanel。
我想画成 3 个面板。这就是我的代码必须是什么样的,我做错了什么?
执行后只有一个空框。
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Thumbnail extends JPanel {
private JPanel bigPanel;
private JPanel panelOne;
private JPanel panelTwo;
private JPanel panelThree;
public Thumbnail() {
panelOne = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.drawString("writing something", 10, 10);
g.drawRect(10, 10, 50, 50);
}
};
bigPanel = new JPanel();
bigPanel.add(panelOne);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.add(bigPanel);
//other codes...
}
public static void main(String[] args) {
JFrame jf = new JFrame();
Thumbnail tb = new Thumbnail();
jf.add(tb);
jf.pack();
jf.setVisible(true);
}
}
bigPanel = new JPanel(new GridLayout(3,1));
bigPanel.add(panelOne, BorderLayout.NORTH);
bigPanel.add(panelTwo, BorderLayout.CENTER);
bigPanel.add(panelThree, BorderLayout.SOUTH);
。
.
this.getContentPane().add(bigPanel);
编辑:
最好扩展一个 JDialog,然后添加 JFrame,然后添加所有面板,只有在目的是使用 JDialog 作为容器的情况下,否则,您的代码没问题。
panelOne
没有向布局管理器提供任何布局信息,这意味着默认情况下,它的首选大小是 0x0
尝试添加类似...
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
至panelOne
...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Thumbnail extends JPanel {
private JPanel bigPanel;
private JPanel panelOne;
private JPanel panelTwo;
private JPanel panelThree;
public Thumbnail() {
panelOne = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.drawString("writing something", 10, 10);
g.drawRect(10, 10, 50, 50);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
};
bigPanel = new JPanel();
bigPanel.add(panelOne);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.add(bigPanel);
//other codes...
}
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 jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Thumbnail tb = new Thumbnail();
jf.add(tb);
jf.pack();
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
});
}
}
您可能想查看 Initial Threads and Laying Out Components Within a Container 了解更多详情
我有一个 class,它扩展了包含 3 个其他面板的 JPanel。 我想画成 3 个面板。这就是我的代码必须是什么样的,我做错了什么? 执行后只有一个空框。
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Thumbnail extends JPanel {
private JPanel bigPanel;
private JPanel panelOne;
private JPanel panelTwo;
private JPanel panelThree;
public Thumbnail() {
panelOne = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.drawString("writing something", 10, 10);
g.drawRect(10, 10, 50, 50);
}
};
bigPanel = new JPanel();
bigPanel.add(panelOne);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.add(bigPanel);
//other codes...
}
public static void main(String[] args) {
JFrame jf = new JFrame();
Thumbnail tb = new Thumbnail();
jf.add(tb);
jf.pack();
jf.setVisible(true);
}
}
bigPanel = new JPanel(new GridLayout(3,1));
bigPanel.add(panelOne, BorderLayout.NORTH);
bigPanel.add(panelTwo, BorderLayout.CENTER);
bigPanel.add(panelThree, BorderLayout.SOUTH);
。 .
this.getContentPane().add(bigPanel);
编辑:
最好扩展一个 JDialog,然后添加 JFrame,然后添加所有面板,只有在目的是使用 JDialog 作为容器的情况下,否则,您的代码没问题。
panelOne
没有向布局管理器提供任何布局信息,这意味着默认情况下,它的首选大小是 0x0
尝试添加类似...
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
至panelOne
...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Thumbnail extends JPanel {
private JPanel bigPanel;
private JPanel panelOne;
private JPanel panelTwo;
private JPanel panelThree;
public Thumbnail() {
panelOne = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.drawString("writing something", 10, 10);
g.drawRect(10, 10, 50, 50);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
};
bigPanel = new JPanel();
bigPanel.add(panelOne);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.add(bigPanel);
//other codes...
}
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 jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Thumbnail tb = new Thumbnail();
jf.add(tb);
jf.pack();
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
});
}
}
您可能想查看 Initial Threads and Laying Out Components Within a Container 了解更多详情