在多个 JPanel 内绘图
我在主框架内有三个 JPanel。从左侧顺时针方向,在第一个面板中,我计划有一些控制面板 2 上的绘图的控件。第三个底部面板将显示一些信息。
我的理解是,我必须重写paintComponent,以便我可以在第二个面板上实现所需的效果。现在我只是想测试一下,是否可以在这个面板上绘制简单的文字。
但事实上,我在三个面板中的任何一个中绘制任何内容时都遇到问题。
下面给出了代码,不知道问题出在哪里。
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);
}
}
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.
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有 JPanel 变量都引用普通的 JPanel,而不是重写的类。
所以考虑将其更改
为:
All your JPanel variables refer to plain vanilla JPanels, not to the overridden classes.
so consider changing this:
to this:
尝试进行以下更改:
在类的构造函数中 -
MyJPanel1
、MyJPanel2
、MyJPanel3
:panel3 = new JPanel();
panel3.set...
替换为this.set...
。并将以下行更改为
:
Try making following changes:
In the constructors for your class -
MyJPanel1
,MyJPanel2
,MyJPanel3
:panel3 = new JPanel();
panel3.set...
withthis.set...
.And change the following lines:
to