在 JFrame 中创建 JPanel,并使用 paintComponent() 方法绘制面板
Creating a JPanel in a JFrame, and drawing the panel, using paintComponent() method
这是我第一次使用java Swing,不知道如何在JSplitPane
中具体JPanel
画画,尝试新建一个class实现paintComponent
方法,但不能Override。
有人可以帮助我吗?
import javax.swing.*;
import java.awt.*;
public class SplitPane extends JPanel{
private JPanel mainPanel;
private JPanel leftPanel;
private JPanel rightPanel;
public SplitPane() {
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SplitPane().createAndShowUI();
}
});
}
private void createAndShowUI() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
initComponents(frame.getContentPane());
frame.setVisible(true);
}
private void initComponents(Container contentPane) {
mainPanel = new JPanel();
leftPanel = new JPanel();
rightPanel = new JPanel();
leftPanel.add(new JLabel("left"));
rightPanel.add(new JLabel("right"));
leftPanel.setPreferredSize(new Dimension(200, 40));
rightPanel.setPreferredSize(new Dimension(280, 400));
leftPanel.setBackground(Color.WHITE);
rightPanel.setBackground(Color.WHITE);
JSplitPane mainJsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
mainJsp.add(leftPanel, JSplitPane.TOP);
mainJsp.add(rightPanel, JSplitPane.BOTTOM);
mainJsp.setOneTouchExpandable(true);
mainJsp.setDividerLocation(150);
mainPanel.add(mainJsp);
contentPane.add(mainPanel);
leftPanel = new PaintPanel();
}
public class PaintPanel extends JPanel {
public PaintPanel() {
System.out.println("PaintPanel");
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(300, 300));
}
@Override
public void paintComponent(Graphics g) {
System.out.println("12345678");
super.paintComponent(g);
//g.setColor(Color.black);
g.drawRect(3, 3, 20, 20);
}
}
}
您永远不会将 PaintPanel
添加到任何内容,例如...
private void initComponents(Container contentPane) {
mainPanel = new JPanel();
leftPanel = new JPanel();
rightPanel = new JPanel();
leftPanel.add(new JLabel("left"));
rightPanel.add(new JLabel("right"));
leftPanel.setPreferredSize(new Dimension(200, 40));
rightPanel.setPreferredSize(new Dimension(280, 400));
leftPanel.setBackground(Color.WHITE);
rightPanel.setBackground(Color.WHITE);
JSplitPane mainJsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
mainJsp.add(leftPanel, JSplitPane.TOP);
mainJsp.add(rightPanel, JSplitPane.BOTTOM);
mainJsp.setOneTouchExpandable(true);
mainJsp.setDividerLocation(150);
mainPanel.add(mainJsp);
contentPane.add(mainPanel);
leftPanel = new PaintPanel();
// Just left hanging here, never added to anything...?
}
因此,如果我将其更改为...
private void initComponents(Container contentPane) {
mainPanel = new JPanel();
leftPanel = new PaintPanel();
rightPanel = new JPanel();
leftPanel.add(new JLabel("left"));
rightPanel.add(new JLabel("right"));
leftPanel.setPreferredSize(new Dimension(200, 40));
rightPanel.setPreferredSize(new Dimension(280, 400));
leftPanel.setBackground(Color.WHITE);
rightPanel.setBackground(Color.WHITE);
JSplitPane mainJsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
mainJsp.add(leftPanel, JSplitPane.TOP);
mainJsp.add(rightPanel, JSplitPane.BOTTOM);
mainJsp.setOneTouchExpandable(true);
mainJsp.setDividerLocation(150);
mainPanel.add(mainJsp);
contentPane.add(mainPanel);
//leftPanel = new PaintPanel();
}
现在显示...
这是我第一次使用java Swing,不知道如何在JSplitPane
中具体JPanel
画画,尝试新建一个class实现paintComponent
方法,但不能Override。
有人可以帮助我吗?
import javax.swing.*;
import java.awt.*;
public class SplitPane extends JPanel{
private JPanel mainPanel;
private JPanel leftPanel;
private JPanel rightPanel;
public SplitPane() {
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SplitPane().createAndShowUI();
}
});
}
private void createAndShowUI() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
initComponents(frame.getContentPane());
frame.setVisible(true);
}
private void initComponents(Container contentPane) {
mainPanel = new JPanel();
leftPanel = new JPanel();
rightPanel = new JPanel();
leftPanel.add(new JLabel("left"));
rightPanel.add(new JLabel("right"));
leftPanel.setPreferredSize(new Dimension(200, 40));
rightPanel.setPreferredSize(new Dimension(280, 400));
leftPanel.setBackground(Color.WHITE);
rightPanel.setBackground(Color.WHITE);
JSplitPane mainJsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
mainJsp.add(leftPanel, JSplitPane.TOP);
mainJsp.add(rightPanel, JSplitPane.BOTTOM);
mainJsp.setOneTouchExpandable(true);
mainJsp.setDividerLocation(150);
mainPanel.add(mainJsp);
contentPane.add(mainPanel);
leftPanel = new PaintPanel();
}
public class PaintPanel extends JPanel {
public PaintPanel() {
System.out.println("PaintPanel");
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(300, 300));
}
@Override
public void paintComponent(Graphics g) {
System.out.println("12345678");
super.paintComponent(g);
//g.setColor(Color.black);
g.drawRect(3, 3, 20, 20);
}
}
}
您永远不会将 PaintPanel
添加到任何内容,例如...
private void initComponents(Container contentPane) {
mainPanel = new JPanel();
leftPanel = new JPanel();
rightPanel = new JPanel();
leftPanel.add(new JLabel("left"));
rightPanel.add(new JLabel("right"));
leftPanel.setPreferredSize(new Dimension(200, 40));
rightPanel.setPreferredSize(new Dimension(280, 400));
leftPanel.setBackground(Color.WHITE);
rightPanel.setBackground(Color.WHITE);
JSplitPane mainJsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
mainJsp.add(leftPanel, JSplitPane.TOP);
mainJsp.add(rightPanel, JSplitPane.BOTTOM);
mainJsp.setOneTouchExpandable(true);
mainJsp.setDividerLocation(150);
mainPanel.add(mainJsp);
contentPane.add(mainPanel);
leftPanel = new PaintPanel();
// Just left hanging here, never added to anything...?
}
因此,如果我将其更改为...
private void initComponents(Container contentPane) {
mainPanel = new JPanel();
leftPanel = new PaintPanel();
rightPanel = new JPanel();
leftPanel.add(new JLabel("left"));
rightPanel.add(new JLabel("right"));
leftPanel.setPreferredSize(new Dimension(200, 40));
rightPanel.setPreferredSize(new Dimension(280, 400));
leftPanel.setBackground(Color.WHITE);
rightPanel.setBackground(Color.WHITE);
JSplitPane mainJsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
mainJsp.add(leftPanel, JSplitPane.TOP);
mainJsp.add(rightPanel, JSplitPane.BOTTOM);
mainJsp.setOneTouchExpandable(true);
mainJsp.setDividerLocation(150);
mainPanel.add(mainJsp);
contentPane.add(mainPanel);
//leftPanel = new PaintPanel();
}
现在显示...