问题描述
我在一个主框架内有三个JPanel.我计划在第一个面板的左侧按顺时针方向设置一些控件,这些控件决定了面板2上的绘图.第三个底部面板将显示一些信息.
I have three JPanels inside a main Frame. Clockwise from the left, in the first panel I plan to have some controls which dictate the drawing on the panel 2. The third bottom panel will show some informations.
我了解的是,我必须重写paintComponent以便在第二个面板上实现所需的效果.现在我只想测试一下,是否可以在此面板上绘制简单的文本.
What I understand is, I have to override paintComponent so that I can achieve the desired effect on the second panel. Right now I just want to test it, whether I can draw simple texts on this panel.
但实际上,我在绘制三个面板中的任何一个时遇到问题.
But in fact, I am having problem to draw anything in any of the three panels.
下面给出了代码,我不知道是什么问题.
The code is given below, I dont know what is the problem.
public class Demo extends JFrame{
private JSplitPane splitPaneV;
private JSplitPane splitPaneH;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
public Demo(String string) {
// TODO Auto-generated constructor stub
setTitle(string);
setBackground( Color.gray );
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create the panels
new MyJPanel1();
new MyJPanel2();
new MyJPanel3();
// Create a splitter pane
splitPaneV = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
topPanel.add( splitPaneV, BorderLayout.CENTER );
splitPaneH = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT );
splitPaneH.setLeftComponent( panel1 );
splitPaneH.setRightComponent( panel2 );
splitPaneV.setLeftComponent( splitPaneH );
splitPaneV.setRightComponent( panel3 );
/*setContentPane(new MyJPanel1());
setContentPane(new MyJPanel2());
setContentPane(new MyJPanel3());*/
}
private class MyJPanel1 extends JPanel{
public MyJPanel1(){
panel1 = new JPanel();
panel1.setLayout( new BorderLayout() );
panel1.setPreferredSize(new Dimension(200,500));
}
@Override
public void paintComponent(Graphics g) {
//super.paintComponent(g);
g.drawString("BLAH1", 20, 20);
g.drawRect(200, 200, 200, 200);
//setVisible(true);
}
}
private class MyJPanel2 extends JPanel{
public MyJPanel2(){
panel2 = new JPanel();
panel2.setLayout( new FlowLayout() );
panel2.setPreferredSize(new Dimension(1000,500));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("BLAH2", 20, 20);
g.drawRect(200, 200, 200, 200);
}
}
private class MyJPanel3 extends JPanel{
public MyJPanel3(){
panel3 = new JPanel();
panel3.setLayout( new BorderLayout() );
panel3.setPreferredSize( new Dimension( 400, 100 ) );
panel3.setMinimumSize( new Dimension( 100, 50 ) );
}
@Override
public void paintComponent(Graphics g) {
//super.paintComponent(g);
g.drawString("BLAH3", 20, 20);
g.drawRect(200, 200, 200, 200);
}
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
//Create and set up the window.
Demo frame = new Demo("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(50, 50, 1200, 700);
frame.pack();
frame.setVisible(true);
}
}
推荐答案
您所有的JPanel变量都引用普通的JPanels,而不是覆盖的类.
All your JPanel variables refer to plain vanilla JPanels, not to the overridden classes.
因此请考虑更改此内容:
so consider changing this:
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
public Demo(String string) {
// ...
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create the panels
new MyJPanel1();
new MyJPanel2();
new MyJPanel3();
// ...
}
private class MyJPanel1 extends JPanel{
public MyJPanel1(){
panel1 = new JPanel();
panel1.setLayout( new BorderLayout() );
panel1.setPreferredSize(new Dimension(200,500));
}
@Override
public void paintComponent(Graphics g) {
//super.paintComponent(g);
g.drawString("BLAH1", 20, 20);
g.drawRect(200, 200, 200, 200);
//setVisible(true);
}
}
//.... etc
对此:
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
public Demo(String string) {
// ...
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create the panels
panel1 = new MyJPanel1();
panel2 = new MyJPanel2();
panel3 = new MyJPanel3();
// ...
}
private class MyJPanel1 extends JPanel{
public MyJPanel1(){
// panel1 = new JPanel();
setLayout( new BorderLayout() );
setPreferredSize(new Dimension(200,500));
}
@Override
public void paintComponent(Graphics g) {
//super.paintComponent(g);
g.drawString("BLAH1", 20, 20);
g.drawRect(200, 200, 200, 200);
//setVisible(true);
}
}
//.... do the same with the other JPanel classes
这篇关于在多个JPanels中绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!