gpt4 book ai didi

java - 图形未出现在 CardLayout 上

转载 作者:行者123 更新时间:2023-12-01 19:41:42 25 4
gpt4 key购买 nike

我正在构建一个使用 CardLayout 在屏幕之间切换的 GUI。我希望其中一个屏幕上有图形。我已经成功地能够使用 GridLayout 将矩形放在面板上,然后将该面板放在主面板上,并且矩形清晰可见。但是,当我将该面板放到 CardLayout 面板上时,矩形就看不到了。将图形添加到 CardLayout 面板时是否需要做一些不同的事情?

主类

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)。然后,当您绘制组件时,您将在 Rectangle(400, 400, 50, 50) 中绘制矩形。使用构造函数Rectangle(int x, int y, int width, int height) .

话虽如此,我认为这是非常明显的。您的组件为 100x100,但您尝试在第 400x400 个像素处绘制,这会导致在组件边界之外绘制矩形。

我不知道矩形的坐标应该是什么,但如果您希望显示组件,请将矩形更改为 Rectangle rect1 = new Rectangle(0, 0, 50, 50);

此外,VGR 在评论中提到(他明白了我:P)当你 overridepaintComponent方法,请确保您调用 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();
});
}

关于java - 图形未出现在 CardLayout 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55199318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com