我想绘制自己的JButton版本,所以我重写了paintComponent()方法,并画了一个渐变roundRect.这可行,但在那之后,我想在它上面绘制Button的字符串,并在编译时,我没有错误消息.但是在运行时,我只看到了roundRect,渐变,就像我想要的那样(我也可以点击它),但是String是不可见的......
这是我的代码:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class JIconButton extends JButton implements MouseListener
{
private boolean mouseInside;
public JIconButton(String file, String text)
{
super(text, new ImageIcon(file));
setBorder(new LineBorder(Color.LIGHT_GRAY, 0, true));
setContentAreaFilled(false);
setFocusPainted(false);
addMouseListener(this);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
@Override
protected void paintComponent(Graphics g)
{
Graphics2D g2 = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用单个按钮获取一个简单的JFrame,以便在发生任何这些事件时触发事件:
似乎Enter和空格键是"免费",以及使用JButton上的addActionListener默认鼠标点击; 问题是,我已经读过,键绑定依赖于所使用的外观.
我试图通过将Enter和空格键添加到JButton的动作映射来在LaF中获得通用行为,甚至添加了一个随机键("m")以确保ActionMap正在进行工作(它是),但现在是鼠标点击丢失.我似乎能够获得所有键和鼠标单击的唯一方法是使用动作映射和addActionListener.
有没有办法让这些键和鼠标绑定在所有LaF中一致地工作,而不试图检测可能出现的每个可能的LaF?我可以注册一个动作侦听器,它将触发键和鼠标事件吗?
我最喜欢的解决方案是在JButton动作图中添加鼠标点击,并检测动作内部发生的键或鼠标点击.
我还在这里学习绳索,所以这可能不是最好或最有效的做事方式; 我确定它过度设计了.这是一种训练练习,我正在试验我能掌握的一切.欢迎任何和所有编码风格的评论!
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
public class Example extends JFrame {
// ============================
private class BtnListener extends AbstractAction {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent ae) {
System.out.println("\nclick button listener triggered");
System.out.println(ae.getSource().getClass().toString());
}
} // class BtnListener
private static final int NO_MODIFIER = 0;
private static …Run Code Online (Sandbox Code Playgroud) 我正在使用swing制作java桌面应用程序.我想把png设置为jbutton.但我无法设置透明图像.我想在android中像set background null那样做,所以可以设置透明图像.
下面的代码是在选择3个按钮中的任何一个时更改背景颜色:红色,绿色或蓝色.当我选择其中任何一个时,实际上什么也没发生.但是,从JButtons更改为JRadioButtons或JToggleButtons确实有效.谁知道为什么?是因为JButton.isselected()方法被窃听并且它总是返回false?我感谢任何帮助......谢谢.
public class bgcolor2 extends JFrame
{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT= 400;
private ActionListener listener;
private JButton greenbutton;
private JButton redbutton;
private JButton bluebutton;
private JPanel colorpanel;
private JPanel buttonpanel;
public bgcolor2()
{
colorpanel = new JPanel();
add(colorpanel,BorderLayout.CENTER);
class bgcolorlistener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
changebgcolor();
}
}
listener=new bgcolorlistener();
createbuttons();
setSize(FRAME_WIDTH,FRAME_HEIGHT);
}
public void createbuttons()
{
greenbutton = new JButton("Green");
greenbutton.addActionListener(listener);
bluebutton = new JButton("Blue");
bluebutton.addActionListener(listener);
redbutton = …Run Code Online (Sandbox Code Playgroud) 我正在尝试JButton使用图标和一些文本创建一个.我的问题是我希望图标左对齐并且文本右对齐(没有必要右对齐,但我不希望文本粘在图标上).
我自己无法做到这一点,我尝试了一个略有不同的解决方案.我用它iconTextGap在图标和文本之间创建了一些空间,原则上工作得很好但是当我创建多个按钮时,它们都具有最宽的宽度,图标不再在左边(除了在带有最长文本的按钮).
我提供了一个代码来证明:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;
import java.net.MalformedURLException;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Main{
private JFrame frame;
private JPanel buttonPanel;
private GridBagConstraints constraints;
public Main() throws MalformedURLException{
frame = new JFrame();
buttonPanel = new JPanel();
frame.add(buttonPanel);
buttonPanel.setLayout(new GridBagLayout());
constraints = new GridBagConstraints();
constraints.insets = new Insets(5, 5, 3, 5);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 0;
String[] text = { …Run Code Online (Sandbox Code Playgroud) 如何获取默认边框JButton?
一个例子是:
Border border = new JButton().getBorder();
Run Code Online (Sandbox Code Playgroud)
但是我可以不创建新按钮吗?
我必须写一个纸牌游戏.单击一张卡时会生成随机卡片图像,但由于您只能单击该卡片一次,因此单击该按钮后将被禁用.如何在卡片图像被点击后停止变灰,以便新生成的卡片图像清晰可见?
//Actions performed when an event occurs
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == card1)
{
randomInteger();
card1.setIcon(cardImages[randomInt]);
card1.setEnabled(false);
}
else if (e.getSource() == card2)
{
randomInteger();
card2.setIcon(cardImages[randomInt]);
card2.setEnabled(false);
}
else if (e.getSource() == card3)
{
randomInteger();
card3.setIcon(cardImages[randomInt]);
card3.setEnabled(false);
}
else if (e.getSource() == card4)
{
randomInteger();
card4.setIcon(cardImages[randomInt]);
card4.setEnabled(false);
}
else
{
randomInteger();
card5.setIcon(cardImages[randomInt]);
card5.setEnabled(false);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我试图将文本分配给循环内的按钮,但我得到一个NullPointerException.我已经在类的构造函数中初始化了按钮数组,在initilizatoin之后,我调用了以下方法.这是我收到错误的代码.
public class Alfred
{
private String names[]={"nfs","gta","maxpayne","hitman"};
private JButton but[];
public Alfred()
{
...
but= new JButton[4];
AssignLettersToButtons();
}
private void AssignLettersToButtons()
{
for(int i=0;i<names.length;i++)
{
but[i].setText(names[i]); // error
pane1.add(but[i]);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
按钮数组和名称数组的长度相同.这个例外的原因是什么,我该如何解决?
问候
我想将鼠标悬停在我的GUI(地图)上的一些JButton上,并显示该位置的名称,例如曼彻斯特和伦敦.我有一个按钮的代码,但它不适用于多个按钮,并打印out所有按钮位置的最后一条消息(因为我有10个按钮).
如果button1为true,则通过我的paintComponent()方法在指定区域中的GUI上绘制文本.
我该如何解决这个问题?
button1.addMouseMotionListener(this);
button2.addMouseMotionListener(this);
Run Code Online (Sandbox Code Playgroud)
public void mouseMoved(MouseEvent arg0)
{
if(button1.contains(arg0.getPoint()))
{
button1 = true;
out = "test 1";
repaint();
}
if(!button1.contains(arg0.getPoint()))
{
b1 = false;
out = " ";
repaint();
}//same for all 10 buttons but change variables
}
Run Code Online (Sandbox Code Playgroud) 这是我的代码,这是非常简单,它只是创造了JFrame一个JTextArea在中心.
if(!txtSource.getText().trim().equals("") && txtSource != null)
即使我在JTextArea中输入了文本,也永远不会满足.
如果JTextArea有一些文本,我只想执行methodA().
private Container content;
private JTextArea txtSource;
public Test() {
this.setTitle("Test");
this.setSize(600,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
content = this.getContentPane();
content.add(getTextArea(), BorderLayout.CENTER);
content.add(button(), BorderLayout.SOUTH);
this.setVisible(true);
}
private JTextArea getTextArea() {
JTextArea txtSource = new JTextArea(20, 80);
return txtSource;
}
private JButton button() {
JButton btn = new JButton("Click me");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!txtSource.getText().trim().equals("") && txtSource != null) {
methodA();
} else {
System.out.println("Please paste your script …Run Code Online (Sandbox Code Playgroud)