当我尝试使用setLayout(null)时,我得到一个普通的灰色屏幕,我的组件都没有.我是否需要为ColorPanel中的每个组件提供x,y值?
import javax.swing.*;
import java.awt.*;
public class GUI{
public static void main(String[] args){
JFrame GUI = new JFrame();
GUI.setLayout(null);
GUI.setTitle("Betrai");
GUI.setSize(500, 500);
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ColorPanel panel = new ColorPanel(Color.white);
Container pane = GUI.getContentPane();
JButton continueButton = new JButton("TEST");
continueButton.setBackground(Color.red);
continueButton.setBounds(10, 10, 60, 40);
pane.add(continueButton);
GUI.setVisible(true);
GUI.setBounds(0, 0, 500, 500);
GUI.setResizable(false);
}
}
Run Code Online (Sandbox Code Playgroud) 出于某种原因,当我尝试从动作中的另一个类调用非静态方法时,我得到一个错误,说我不能在静态方法中调用非静态方法.但是,我从未将动作或其父母定义为静态.为什么我会收到此错误?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class KeyListenerFrame extends JFrame implements KeyListener {
GridLayout gridLayout1 = new GridLayout();
JPanel listenerPan = new JPanel();
public KeyListenerFrame() {
try {
jbInit();
listenerPan.addKeyListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
listenerPan.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP,0,true), "showKey");
listenerPan.getActionMap().put("showKey", showKey);
listenerPan.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "showKey");
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
KeyListenerFrame klf = new KeyListenerFrame();
klf.setBounds(10,10,500,500);
klf.setVisible(true);
klf.focusPanel();
}
public void focusPanel() {
listenerPan.requestFocus();
}
private …Run Code Online (Sandbox Code Playgroud) 有没有办法轻松地从屏幕上删除ImageIcon?我找不到任何地方.
ImageIcon image = new ImageIcon("candle.gif");
image.paintIcon(this, g, 150, 80)
Run Code Online (Sandbox Code Playgroud)
例如,如果我想在以后按下按钮时摆脱"图像",那么适当的代码是什么?(不是按钮,我知道怎么做).