我正在构建一个使用卡套件在屏幕之间切换的GUI。我希望其中一个屏幕上有图形。我已经成功地将矩形放在带有Gridlayout的面板上,并将面板放在主面板上,并且矩形清晰可见。但是,当我将该面板放在纸牌仪面板上时,矩形无处可见。将图形添加到纸牌层面板中时是否需要做些不同的事情?
主要类
import java.awt.CardLayout;
import java.awt.Container;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;
public class Window extends JFrame {
public Window() {
// Main Panel Configuration
Container panel = this.getContentPane();
panel.setLayout(new GridBagLayout());
panel.setSize(1000,1000);
GridBagConstraints gbc = new GridBagConstraints();
// GridBag Constraints
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 0;
gbc.gridy = 0;
// Button Tabs
JButton button1 = new JButton("button 1");
panel.add(button1, gbc);
JButton button2 = new JButton("button 2");
gbc.gridx = 1;
panel.add(button2, gbc);
// Panels for CardLayout
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
// Set Layout of Panels
panel1.setLayout(new GridBagLayout());
panel2.setLayout(new GridBagLayout());
// Set Dimensions of Panels
panel1.setSize(new Dimension(900, 500));
panel2.setSize(new Dimension(900, 500));
// Labels for panels
JLabel label1 = new JLabel("label 1");
gbc.gridx = 1;
gbc.gridy = 2;
panel1.add(label1, gbc);
JLabel label2 = new JLabel("label 2");
gbc.gridy = 2;
panel2.add(label2, gbc);
// TextFields for Panels
JTextField textField1 = new JTextField();
gbc.gridx = 2;
gbc.gridy = 2;
panel1.add(textField1, gbc);
JTextField textField2 = new JTextField();
gbc.gridy = 2;
panel2.add(textField2, gbc);
// Add Graphic to panel1
JPanel drawingPanel = new JPanel();
DrawingComponent dc = new DrawingComponent();
dc.setPreferredSize(new Dimension(100, 100));
drawingPanel.add(dc);
gbc.gridx = 0;
gbc.gridy = 3;
panel1.add(drawingPanel,gbc);
// Set TextField Dimensions
textField1.setPreferredSize(new Dimension(400, 20));
textField2.setPreferredSize(new Dimension(400, 20));
// CardLayout Panel for Entry Fields and Labels
JPanel cardLayoutPanel = new JPanel();
CardLayout c1 = new CardLayout();
cardLayoutPanel.setLayout(c1);
cardLayoutPanel.setSize(new Dimension(900, 600));
// Add Panels to CardLayout Panel
cardLayoutPanel.add("panel1",panel1);
cardLayoutPanel.add("panel2", panel2);
// Which panel to show First
c1.show(cardLayoutPanel, "panel1");
// Add CardLayout Panel to Main Panel
gbc.gridy = 1;
gbc.gridx = 0;
panel.add(cardLayoutPanel, gbc);
// Action Listeners Changing Between Tabs
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
c1.show(cardLayoutPanel, "panel1");
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
c1.show(cardLayoutPanel, "panel2");
}
});
setSize(800,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args){
Window window = new Window();
}
}
图形组件类
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import java.awt.Rectangle;
public class DrawingComponent extends JComponent {
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
Rectangle rect1 = new Rectangle(400, 400, 50, 50);
g2.draw(rect1);
}
}
在此部分:
JPanel drawingPanel = new JPanel();
DrawingComponent dc = new DrawingComponent();
dc.setPreferredSize(new Dimension(100, 100));
drawingPanel.add(dc);
您将DrawingComponent
的首选尺寸设置为(100,100)。然后,当您绘制组件时,您可以使用conthortor Rectangle(int x, int y, int width, int height)
在Rectangle(400, 400, 50, 50)
中绘制直流。
话虽如此,我认为这很明显。您的组件是100x100,但您尝试在400thx400th的像素上绘制,这会导致矩形在组件的边界外绘制。
我不知道矩形的坐标是什么,但是如果您希望显示组件,请将矩形更改为 Rectangle rect1 = new Rectangle(0, 0, 50, 50);
另外,评论中提到的VGR(他得到了我:P)当您override
paintComponent
方法时,请确保致电super.paintComponent()
。
最后,使用SwingUtilities#invokeLater
方法开始您的应用程序。
我所说的代码的总和:
public static class DrawingComponent extends JComponent {
public void paintComponent(Graphics g) {
super.paintComponent(g); // Should always start with super method.
Graphics2D g2 = (Graphics2D) g;
Rectangle rect1 = new Rectangle(0, 0, 50, 50);
g2.draw(rect1);
}
}
public static void main(String[] args) {
// All swing applications must run on their own thread named EDT (Event dispatch thread)
SwingUtilities.invokeLater(() -> {
Window window = new Window();
});
}
一个问题是您在组件的边界之外绘制。当组件本身的大小为100×100时,在(400,400)处绘制矩形将一无所获。
另一个问题是您正在尝试明确设置组件和窗口的尺寸。但是,对于这些尺寸,窗户还不够大。当GridBaglayout无法容纳其孩子的首选尺寸时,它"放弃"并将所有内容设置为最小尺寸。由于DrawsComponent并未定义最小尺寸,因此它恢复为零宽度和零高度。(您对填充的使用=水平力与单元一样宽,但高度仍然为零。)
设置所有这些尺寸是不正确的。几乎从来没有有正当理由调用设置或setPreferredSize。
对于jtextfield,您需要使用其setColumns方法设置其大小。
drauctComponent类应该知道它想要的大小,并且应该通过覆盖自己的GetPreferredSize方法来返回该大小。显然,它必须足够大才能容纳自己的绘画:
@Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
对于面板和窗口,请勿完全设置大小。如果您构建整个窗口,然后在上面拨打Pack(),他们都将足够大以容纳他们的孩子组件。
。再次,应该没有调用代码中设置或设置setPreferredsize的调用。您不需要它们。