按钮和 JSlider 仅在我将鼠标悬停在屏幕上时出现
Button and JSlider only appear when I hover the screen
谁能帮我指点一下我的错误在哪里?当我将鼠标悬停时,按钮和 JSlider 出现,这让我很紧张。当我使用按钮时它很好,但是一旦我添加一个按钮,JSlider 就会丢失并在我悬停并单击时出现。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
public class MainDisplay extends JFrame implements MouseMotionListener,
MouseListener, ChangeListener, ActionListener{
private Point mousepnt = new Point();
public static Color penColor = new Color(255,105,180);
private final JSlider penSize = new JSlider(JSlider.HORIZONTAL, 1, 30, 4);
public static int pen = 4;
JButton rbutton = new JButton();
JButton cbutton = new JButton();
JButton tbutton = new JButton();
JButton qbutton = new JButton();
ImageIcon ricon = new ImageIcon(getClass().getResource("ricon.png"));
ImageIcon cicon = new ImageIcon(getClass().getResource("cicon.png"));
ImageIcon ticon = new ImageIcon(getClass().getResource("ticon.png"));
ImageIcon qicon = new ImageIcon(getClass().getResource("qicon.png"));
public MainDisplay(){
JPanel toolbar = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel toolbar2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel jp = new JPanel();
jp.addMouseMotionListener(this);
jp.addMouseListener(this);
toolbar.add(new Label("Drag mouse to draw"));
toolbar.add(penSize);
penSize.addChangeListener(this);
toolbar2.add(new Label("Choose shape to draw"));
rbutton = new JButton(ricon);
rbutton.addActionListener(this);
rbutton.setMargin(new Insets(1, 2, 1, 2));
toolbar2.add(rbutton);
cbutton = new JButton(cicon);
cbutton.addActionListener(this);
cbutton.setMargin(new Insets(1, 2, 1, 2));
toolbar2.add(cbutton);
tbutton = new JButton(ticon);
tbutton.addActionListener(this);
tbutton.setMargin(new Insets(1, 2, 1, 2));
toolbar2.add(tbutton);
qbutton = new JButton(qicon);
qbutton.addActionListener(this);
qbutton.setMargin(new Insets(1, 2, 1, 2));
toolbar2.add(qbutton);
this.add(toolbar,BorderLayout.SOUTH);
this.add(toolbar2,BorderLayout.NORTH);
this.add(jp,BorderLayout.CENTER);
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
主要class
public class Main {
public static void main(String[] args){
MainDisplay md = new MainDisplay();
}
}
我会做什么:
向面板添加 MouseMotionListener:
jp.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
System.out.println("motion" + e.getPoint());
}
});
给 penSize 添加一个 ChangeListener:
penSize.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println("new pensize: " + penSize.getValue());
}
});
等等。所以你已经将 lsiteners 添加到组件而不是框架。
我希望暂时能帮到你。如果您有任何问题,请 post 安德鲁所写的最小可重现示例。
史蒂菲
谁能帮我指点一下我的错误在哪里?当我将鼠标悬停时,按钮和 JSlider 出现,这让我很紧张。当我使用按钮时它很好,但是一旦我添加一个按钮,JSlider 就会丢失并在我悬停并单击时出现。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
public class MainDisplay extends JFrame implements MouseMotionListener,
MouseListener, ChangeListener, ActionListener{
private Point mousepnt = new Point();
public static Color penColor = new Color(255,105,180);
private final JSlider penSize = new JSlider(JSlider.HORIZONTAL, 1, 30, 4);
public static int pen = 4;
JButton rbutton = new JButton();
JButton cbutton = new JButton();
JButton tbutton = new JButton();
JButton qbutton = new JButton();
ImageIcon ricon = new ImageIcon(getClass().getResource("ricon.png"));
ImageIcon cicon = new ImageIcon(getClass().getResource("cicon.png"));
ImageIcon ticon = new ImageIcon(getClass().getResource("ticon.png"));
ImageIcon qicon = new ImageIcon(getClass().getResource("qicon.png"));
public MainDisplay(){
JPanel toolbar = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel toolbar2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel jp = new JPanel();
jp.addMouseMotionListener(this);
jp.addMouseListener(this);
toolbar.add(new Label("Drag mouse to draw"));
toolbar.add(penSize);
penSize.addChangeListener(this);
toolbar2.add(new Label("Choose shape to draw"));
rbutton = new JButton(ricon);
rbutton.addActionListener(this);
rbutton.setMargin(new Insets(1, 2, 1, 2));
toolbar2.add(rbutton);
cbutton = new JButton(cicon);
cbutton.addActionListener(this);
cbutton.setMargin(new Insets(1, 2, 1, 2));
toolbar2.add(cbutton);
tbutton = new JButton(ticon);
tbutton.addActionListener(this);
tbutton.setMargin(new Insets(1, 2, 1, 2));
toolbar2.add(tbutton);
qbutton = new JButton(qicon);
qbutton.addActionListener(this);
qbutton.setMargin(new Insets(1, 2, 1, 2));
toolbar2.add(qbutton);
this.add(toolbar,BorderLayout.SOUTH);
this.add(toolbar2,BorderLayout.NORTH);
this.add(jp,BorderLayout.CENTER);
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
主要class
public class Main {
public static void main(String[] args){
MainDisplay md = new MainDisplay();
}
}
我会做什么:
向面板添加 MouseMotionListener:
jp.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
System.out.println("motion" + e.getPoint());
}
});
给 penSize 添加一个 ChangeListener:
penSize.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println("new pensize: " + penSize.getValue());
}
});
等等。所以你已经将 lsiteners 添加到组件而不是框架。
我希望暂时能帮到你。如果您有任何问题,请 post 安德鲁所写的最小可重现示例。
史蒂菲