Java 重新绘制()JPanel
我目前正在尝试理解Java 重新绘制()JPanel,java,swing,jpanel,Java,Swing,Jpanel,我目前正在尝试理解paintComponent()和repaint()方法,但我就是不能。我有一个带按钮的面板。我想做的是当我点击按钮清除面板(移除按钮)并可能调整其大小,同时添加一个JTextField,但我不知道在paintComponent()方法中写什么 public class Test extends JFrame{ public Test(){ ImageIcon image = new ImageIcon("Buton.png"); JLabel buton =
paintComponent()
和repaint()
方法,但我就是不能。我有一个带按钮的面板。我想做的是当我点击按钮清除面板(移除按钮)并可能调整其大小,同时添加一个JTextField
,但我不知道在paintComponent()方法中写什么
public class Test extends JFrame{
public Test(){
ImageIcon image = new ImageIcon("Buton.png");
JLabel buton = new JLabel(image);
JPanel panel = new JPanel();
buton.addMouseListener(new MouseListener(){
public void mousePressed(MouseEvent e){
ImageIcon image = new ImageIcon("Buton-Pressed.png");
buton.setIcon(image);
remove(panel);
revalidate();
repaint();
}
panel.setOpaque(true);
panel.setBackground(Color.BLACK);
panel.add(buton);
add(panel);
public static void main(String[] args) {
JFrame frame = new Test();
frame.getContentPane().setBackground(Color.BLACK);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public class Paint extends JPanel{
protected void paintComponent(Graphics g){
super.paintComponent(g);
}
}
}
这是MadProgrammer发布后我的鼠标按下事件的代码:
buton.addMouseListener(new MouseListener(){
public void mousePressed(MouseEvent e){
ImageIcon image = new ImageIcon("Buton-Pressed.png");
buton.setIcon(image);
removeAll();
add(new JTextField("Big text field"));
Window window = SwingUtilities.getWindowAncestor(Test.this);
window.pack();
window.setLocationRelativeTo(null);
}
但是我在window.pack()上遇到了这个错误:
paintComponent
与组件管理无关,不应用于在UI中添加或删除组件
MouseListener
与JButton
一起使用是错误的侦听器,用户使用键盘、键盘快捷键和鼠标以及编程方式可能会触发按钮,而不是使用ActionListener
首先,请仔细查看:
所以,根据你的要求,比如
import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
JButton btn = new JButton("Big Button");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
removeAll();
add(new JTextField("Big text field"));
Window window = SwingUtilities.getWindowAncestor(TestPane.this);
window.pack();
window.setLocationRelativeTo(null);
}
});
add(btn);
}
}
}
应该没问题
如果你真的想了解绘画是如何在Swing中工作的,那么你需要更仔细地看一看,从你描述你要做的事情的方式来看,没有理由使用绘画。您没有尝试过向GUI添加JTextField吗?显然,您必须知道如何添加组件,因为您已经添加了panel.add(buton)代码>和添加(面板)代码>。如果你不知道怎么做,那么你应该不要要求我们教你这些基本概念。示例代码还包含语法错误。到底是什么问题?我想在删除按钮的同时添加一个JTextField。(尝试了remove(buton)和removeAll(),但没有成功)并调整框架的大小。此外,它还包含语法错误,因为我在这里快速编辑了它。它工作正常,没有错误。我没有JButton,我有JLabel。谢谢你的回答。我将测试它,并编辑这篇文章,所以我编辑了我的主要文章,让你们看看我得到了什么错误。基本上,我创建一个JLabel是为了创建一个自定义按钮,该按钮在输入、退出和按下时都会发生变化。您确实意识到JButton
已经具有rollOver
支持?这是从SwingUtilities.getWindowSenator(TestPane.this)获得null
结果的唯一原因代码>是因为Test
没有连接到窗口。我知道它支持滚动,但我不喜欢它的设计。我找到了它。为什么现在不能添加frame.setDefaultCloseOperation(退出时关闭)?
import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
JButton btn = new JButton("Big Button");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
removeAll();
add(new JTextField("Big text field"));
Window window = SwingUtilities.getWindowAncestor(TestPane.this);
window.pack();
window.setLocationRelativeTo(null);
}
});
add(btn);
}
}
}