调整窗口大小时,Java Swing图形消失
我非常需要你的帮助,因为我无法独自解决这个问题。 我正在尝试创建一个GUI,并希望在按下按钮后在其中绘制一些东西,但我似乎遇到了某种刷新/重新验证或线程问题。绘图已绘制,但当我调整窗口大小时,绘图将消失。此外,当快速移动窗口时,图形的某些部分也会消失。我试过很多方法,但我不能解决这个问题,也许你能帮我。我被指示一开始不要编写自己的代码,而是使用NetBeans设计函数来生成按钮和面板等。这可能会影响绘图过程/功能,但我不知道。我将向您发布相关代码,并非常感谢您的建议(未发表评论的内容只是我以前尝试过的人工制品,所以不要介意):调整窗口大小时,Java Swing图形消失,java,swing,resize,Java,Swing,Resize,我非常需要你的帮助,因为我无法独自解决这个问题。 我正在尝试创建一个GUI,并希望在按下按钮后在其中绘制一些东西,但我似乎遇到了某种刷新/重新验证或线程问题。绘图已绘制,但当我调整窗口大小时,绘图将消失。此外,当快速移动窗口时,图形的某些部分也会消失。我试过很多方法,但我不能解决这个问题,也许你能帮我。我被指示一开始不要编写自己的代码,而是使用NetBeans设计函数来生成按钮和面板等。这可能会影响绘图过程/功能,但我不知道。我将向您发布相关代码,并非常感谢您的建议(未发表评论的内容只是我以前尝
公共类NewJFrame扩展了JFrame{
公共NewJFrame(){initComponents();}
@抑制警告(“未选中”)
私有组件(){
jButton1=newjavax.swing.JButton();
jPanel1=newjavax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(新java.awt.Dimension(12001000));
jButton1.setText(“绘图”);
jButton1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件evt){
jButton1ActionPerformed(evt);
}
});
GroupLayout jPanel1Layout=新的GroupLayout(jPanel1);
setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0,1000,短。最大值)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0,0,短.MAX_值)
);
GroupLayout=newgrouplayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1,GroupLayout.DEFAULT\u大小,1000,Short.MAX\u值)
.addGap(18,18,18)
.addComponent(jButton1)
.addGap(33,33,33))
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0,745,短。最大值)
.addComponent(jButton1)
.addGap(237237)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1,GroupLayout.PREFERRED\u SIZE,GroupLayout.DEFAULT\u SIZE,GroupLayout.PREFERRED\u SIZE)
.addContainerGap()))
);
包装();
}//
私有void jButton1ActionPerformed(ActionEvent evt){
Graphics g=jPanel1.getGraphics();
绘制(jPanel1,g);
}
受保护组件(图形g){
jPanel1.setSize(10001000);
维度d=jPanel1.getSize();
g、 设置颜色(颜色为黑色);
对于(int i=0;i当您调整面板大小或更改其位置或最小化和最大化面板时,绘制(…)
方法被调用以重新绘制内容。您必须覆盖此函数并让它绘制线条或其他内容。为此,您可能必须将图形保存在数据结构中,以便能够在每次需要时重新绘制
描述了绘制方法。我没有阅读您的所有代码,但立即可以看到的是,您不应该在不调用super.paintComponent
的情况下重写paintComponent
方法。您的注释行调用了super.paintComponents
(注意s),这是不同的
我也不确定“当我调整窗口大小”是什么意思,以及它是如何与Jpanel1.setSize(10001000)联系在一起的
调用,这些调用会出现在整个代码中。您的布局管理器将负责面板的大小。您不应将其设置为固定大小。Java不记得为您绘制命令;单击按钮时,图形会呈现一次,因为这是调用draw()
的唯一时间
如果要在调整大小后刷新渲染,请覆盖paint(Graphics)
并在其中调用draw()
如果这与按钮单击有关,则必须向类中添加字段,以便记住draw()
中需要的所有内容,包括是否应该绘制任何内容:
private boolean drawAtAll = false;
private void jButton1ActionPerformed(ActionEvent evt) {
drawAtAll = true; // ok to draw now
draw();
}
@Override
public void paint(Graphics g) {
super.paint(g);
draw();
}
public void draw() {
if( !drawAtAll ) return;
Graphics g = jPanel1.getGraphics();
...
}
进一步阅读:
尝试使用此代码,并询问可能出现的任何问题,在JPanel的方法中进行绘制。不必每次都为所述JComponent
提供大小,您只需覆盖所述组件的大小。为了调用paintComponent(…)
您只需编写repaint()
Swing将自动执行该操作,而不是从程序中显式调用paintComponent(…)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PaintingExample
{
private CustomPanel paintingPanel;
private Timer timer;
private int x = 1;
private int y = 1;
private ActionListener timerAction = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
x++;
y++;
paintingPanel.setPosition(x, y);
}
};
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("Painting Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
paintingPanel = new CustomPanel();
final JButton startStopButton = new JButton("STOP");
startStopButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (timer.isRunning())
{
startStopButton.setText("START");
timer.stop();
}
else if (!timer.isRunning())
{
startStopButton.setText("STOP");
timer.start();
}
}
});
frame.add(paintingPanel, BorderLayout.CENTER);
frame.add(startStopButton, BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
timer = new Timer(100, timerAction);
timer.start();
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new PaintingExample().createAndDisplayGUI();
}
});
}
}
class CustomPanel extends JPanel
{
private int x = 0;
private int y = 0;
@Override
public Dimension getPreferredSize()
{
return (new Dimension(800, 600));
}
public void setPosition(int a, int b)
{
x = a;
y = b;
if (x <(getWidth() - 10) && y < (getHeight() - 10))
repaint();
else
System.out.println("Nothing is happening...");
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.clearRect(0, 0, getWidth(), getHeight());
g.setColor(Color.MAGENTA);
g.fillOval(x, y, 10, 10);
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类绘画示例
{
私人定制面板绘画面板;
私人定时器;
私有整数x=1;
私有整数y=1;
私有ActionListener timerAction=新ActionListener()
{
已执行的公共无效行动(行动事件ae)
{
x++;
y++;
喷漆面板。设置位置(x,y);
}
};
私有void createAndDisplayGUI()
{
JFrame=新JFrame(“绘画示例”);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
喷漆面板=新客户
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PaintingExample
{
private CustomPanel paintingPanel;
private Timer timer;
private int x = 1;
private int y = 1;
private ActionListener timerAction = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
x++;
y++;
paintingPanel.setPosition(x, y);
}
};
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("Painting Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
paintingPanel = new CustomPanel();
final JButton startStopButton = new JButton("STOP");
startStopButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (timer.isRunning())
{
startStopButton.setText("START");
timer.stop();
}
else if (!timer.isRunning())
{
startStopButton.setText("STOP");
timer.start();
}
}
});
frame.add(paintingPanel, BorderLayout.CENTER);
frame.add(startStopButton, BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
timer = new Timer(100, timerAction);
timer.start();
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new PaintingExample().createAndDisplayGUI();
}
});
}
}
class CustomPanel extends JPanel
{
private int x = 0;
private int y = 0;
@Override
public Dimension getPreferredSize()
{
return (new Dimension(800, 600));
}
public void setPosition(int a, int b)
{
x = a;
y = b;
if (x <(getWidth() - 10) && y < (getHeight() - 10))
repaint();
else
System.out.println("Nothing is happening...");
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.clearRect(0, 0, getWidth(), getHeight());
g.setColor(Color.MAGENTA);
g.fillOval(x, y, 10, 10);
}
}