Java swing - 重绘在重新调整大小时复制组件(JButtons 和 JLabels)
Java swing - Repaint is duplicating components (JButtons and JLabels) when re-sized
我遇到了这个奇怪的问题。我的教授似乎无法复制它,他提供的帮助充其量也很少。所以说到点子上。此代码在面板上生成 JButton 并添加到 JFrame 的内容窗格:
public myPanel extends JPanel {
static final long serialVersionUID = 1;
public myPanel() {
this.setPreferredSize(new Dimension(600, 40));
}
public void paintComponent(Graphics graphicsDrawer) {
super.paintComponent(graphicsDrawer);
this.add(new JButton("A Button"));
}
}
这是 GUI 代码
public class myGUI {
public myGUI() {
}
public void showGUI() {
JFrame frame = new JFrame("Issues!!!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new myPanel());
frame.pack();
frame.setVisible(true);
}
}
然后GUI就是运行这个class
// It is unnecessary to show here but I made a class that
// implements runnable that creates a myGUI object and calls
// showGUI
public static void main(String[] args) {
RunnableGUI runGUI = new RunnableGUI();
javax.swing.SwingUtilities.invokeLater(runGUI);
}
这是我的代码 最低 。然而,我已经隔离了这一点,即使有了最基本的要素,问题仍然存在。下面是我遇到问题的照片。
Vertical Re-size creates extra buttons
我已经尝试了所有我能找到和想到的东西。
我认为框架每次重新调整大小时都会重新绘制按钮。但是,由于分配要求,框架必须能够重新调整大小。
运行 Windows 8 w/ JRE 7, 8(我会下载 6,但我认为这不会有帮助)
从不这样做:
public void paintComponent(Graphics graphicsDrawer) {
super.paintComponent(graphicsDrawer);
this.add(new JButton("A Button")); // ***** yikes! ****
}
paintComponent 方法仅用于绘制。您无法完全控制它是否会被调用,甚至它是否会被调用,它可以被调用 多次 次,这意味着要添加很多按钮。此外,您希望避免组件创建代码或 any 代码在绘画方法中减慢程序流程,因为这些方法部分决定了 GUI 的感知响应能力,因此您需要绘画方法(paint
和 paintComponent
)变得精简、平均和 快速.
在构造函数或初始化方法中添加按钮,以便控制调用该代码的频率。
public myPanel extends JPanel {
static final long serialVersionUID = 1;
public myPanel() {
this.setPreferredSize(new Dimension(600, 40));
this.add(new JButton("A Button"));
}
public void paintComponent(Graphics graphicsDrawer) {
super.paintComponent(graphicsDrawer);
}
}
我遇到了这个奇怪的问题。我的教授似乎无法复制它,他提供的帮助充其量也很少。所以说到点子上。此代码在面板上生成 JButton 并添加到 JFrame 的内容窗格:
public myPanel extends JPanel {
static final long serialVersionUID = 1;
public myPanel() {
this.setPreferredSize(new Dimension(600, 40));
}
public void paintComponent(Graphics graphicsDrawer) {
super.paintComponent(graphicsDrawer);
this.add(new JButton("A Button"));
}
}
这是 GUI 代码
public class myGUI {
public myGUI() {
}
public void showGUI() {
JFrame frame = new JFrame("Issues!!!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new myPanel());
frame.pack();
frame.setVisible(true);
}
}
然后GUI就是运行这个class
// It is unnecessary to show here but I made a class that
// implements runnable that creates a myGUI object and calls
// showGUI
public static void main(String[] args) {
RunnableGUI runGUI = new RunnableGUI();
javax.swing.SwingUtilities.invokeLater(runGUI);
}
这是我的代码 最低 。然而,我已经隔离了这一点,即使有了最基本的要素,问题仍然存在。下面是我遇到问题的照片。
Vertical Re-size creates extra buttons
我已经尝试了所有我能找到和想到的东西。
我认为框架每次重新调整大小时都会重新绘制按钮。但是,由于分配要求,框架必须能够重新调整大小。
运行 Windows 8 w/ JRE 7, 8(我会下载 6,但我认为这不会有帮助)
从不这样做:
public void paintComponent(Graphics graphicsDrawer) {
super.paintComponent(graphicsDrawer);
this.add(new JButton("A Button")); // ***** yikes! ****
}
paintComponent 方法仅用于绘制。您无法完全控制它是否会被调用,甚至它是否会被调用,它可以被调用 多次 次,这意味着要添加很多按钮。此外,您希望避免组件创建代码或 any 代码在绘画方法中减慢程序流程,因为这些方法部分决定了 GUI 的感知响应能力,因此您需要绘画方法(paint
和 paintComponent
)变得精简、平均和 快速.
在构造函数或初始化方法中添加按钮,以便控制调用该代码的频率。
public myPanel extends JPanel {
static final long serialVersionUID = 1;
public myPanel() {
this.setPreferredSize(new Dimension(600, 40));
this.add(new JButton("A Button"));
}
public void paintComponent(Graphics graphicsDrawer) {
super.paintComponent(graphicsDrawer);
}
}