小编Asp*_*ope的帖子

Java null布局导致空白屏幕

当我尝试使用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)

java layout swing

2
推荐指数
1
解决办法
2258
查看次数

Java认为方法是静态的?

出于某种原因,当我尝试从动作中的另一个类调用非静态方法时,我得到一个错误,说我不能在静态方法中调用非静态方法.但是,我从未将动作或其父母定义为静态.为什么我会收到此错误?

    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)

java methods static non-static

1
推荐指数
1
解决办法
899
查看次数

Java - 删除ImageIcon?

有没有办法轻松地从屏幕上删除ImageIcon?我找不到任何地方.

ImageIcon image = new ImageIcon("candle.gif");
image.paintIcon(this, g, 150, 80)
Run Code Online (Sandbox Code Playgroud)

例如,如果我想在以后按下按钮时摆脱"图像",那么适当的代码是什么?(不是按钮,我知道怎么做).

java swing imageicon

0
推荐指数
1
解决办法
6879
查看次数

标签 统计

java ×3

swing ×2

imageicon ×1

layout ×1

methods ×1

non-static ×1

static ×1