嗨,我有一个问题,我有一个对象类型为Object Class的对象,我想将其转换为类型为java.swing.JButton的对象吗?这是代码:
private void EventSelectedFromList(java.awt.event.ActionEvent evt) {
// add code here
try
{
String eventName = (String)EventList.getSelectedItem();// get the event
EventSetDescriptor ed = eventValue(events,eventName);
if(ed.getListenerType() == ActionListener.class){// check if selected event has an actionListerner listener
AddEventHandlerButton.setEnabled(true);// Enable eventhandler button
String objectName = (String) ObjectList.getSelectedItem();
Object ob = FindObject(hm, objectName);// retrieve the object from hashmap
// now 'ob' of type of JButton, I want to add ActionListener to this JButton???
Class zzz = ob.getClass();
System.out.println(zzz);
} else {
AddEventHandlerButton.setEnabled(false);
}
}catch(Exception …Run Code Online (Sandbox Code Playgroud) 我有一个类,有一些摆动组件.在第二类(根据要求),我需要模拟最终调用的按钮单击事件actionPerformed(ActionEvent ae).此按钮位于第一个类中(在第一行中描述).
我该怎么办?
我试图通过fireActionPerformed(ActionEvent ae).但是,我无法找到解决方案.一个小片段(作为示例)将非常有用.
编辑注意:当我点击它或模拟它的点击时,我还需要禁用该按钮.
请原谅我,如果之前已经问过这个问题.虽然我之前搜索过类似的问题.
感谢致敬.
我在这里错过了什么?
public class abc extends JFrame {
private JButton save = new JButton("Save");
public abc() {
JPanel p = new JPanel();
save.addActionListener(new SaveL());
p.add(save);
Container cp = getContentPane();
p = new JPanel();
p.setLayout(new GridLayout(2, 1));
cp.add(p, BorderLayout.NORTH);
}
}
class SaveL implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello"); // nothing happens
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我的ActionListener不在这里工作
如何JDialog在点击JButton两次或更多时停止重复打开?
我想要一个圆边的按钮.我的按钮的背景颜色为黄色.我无法获得按钮的圆形边缘.这是我正在尝试的代码
class RoundedBorder implements Border {
int radius;
RoundedBorder(int radius) {
this.radius = radius;
}
public Insets getBorderInsets(Component c) {
return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);
}
public boolean isBorderOpaque() {
return true;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
g.drawRoundRect(x,y,width-1,height-1,radius,radius);
}
}
jButton1.setText(aContinue);
jButton1.setBackground(new java.awt.Color(255, 255, 0));
jButton1.setBorder(new RoundedBorder(20));
Run Code Online (Sandbox Code Playgroud)
我无法使用这段代码获得圆形边缘.这是我的按钮的外观.

我想要圆边没有溢出的背景颜色.
我一直在尝试圆形按钮,但这里有一个问题.目前我得到一个这样的按钮:

我想要做的是填充按钮方块中的其余部分.我该怎么做呢 ?这是绘制此按钮的代码.
import javax.swing.*;
import java.awt.*;
class Tester extends JButton {
public Tester(String label) {
super(label);
setContentAreaFilled(false);
Dimension size = this.getPreferredSize();
size.width = size.height = Math.max(size.width,size.height);
this.setPreferredSize(size);
}
@Override
public void paintComponent(Graphics g) {System.out.println("Inside the paintComponent method");
if (getModel().isArmed()) {
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillOval(0,0,getSize().width-1,getSize().height-1);
System.out.println(getSize().width);
System.out.println(getSize().height);
super.paintComponent(g);
}
public static void main(String args[]) {
JFrame fr = new JFrame();
JPanel p = new JPanel();
JButton button = new Tester("Click Me !");
button.setBackground(Color.GREEN);
p.add(button);
fr.add(p);
fr.setVisible(true);
fr.setSize(400,400); …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,在更改后,出现绿色复选标记,表示更改成功.应用程序有几个可能的更改,我希望能够在2.5秒后使复选标记消失.我尝试过几样的事情:
panel.add(checkMark);
checkMark.setVisible(true);
panel.remove(checkMark);
checkMark.setVisible(false);
Run Code Online (Sandbox Code Playgroud)
似乎没有什么工作.我添加了一个timer电话,接着是a checkMark.setVisible(false),似乎没有任何帮助.
有人可以指出我做错了什么吗?以下是我的代码:
//Create Change Role Button
final JButton changeRoleBtn = new JButton("Change Role");
changeRoleBtn.setBounds(50, 500, 150, 30);
changeRoleBtn.setToolTipText("Changes the role of the User");
changeRoleBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//Create Success Image
final ImageIcon i1 = new ImageIcon("/Users/vhaislsalisc/Documents/workspace/Role_Switcher/greenCheck.png");
final JLabel checkMark = new JLabel(i1);
checkMark.isOptimizedDrawingEnabled();
i1.paintIcon(changeRoleBtn, getGraphics(), 400,25);
checkMark.setVisible(true);
try
{
timer = new Timer(2000, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
checkMark.setVisible(false);
timer.stop();
}
});
timer.start(); …Run Code Online (Sandbox Code Playgroud) 我试图用JBF-8编码将我的JButton的文本设置为"Compute b--¹(mod a)",但这两个尝试都不起作用.问题是减号,但不确定我能做什么.
尝试1:
_computeModInverseButton = new JButton("Compute b?¹ (mod a)");
Run Code Online (Sandbox Code Playgroud)

尝试2:
_computeModInverseButton = new JButton("Compute b<html><sup>-1</sup></html> (mod a)");
Run Code Online (Sandbox Code Playgroud)

所以问题是我的HTML格式,但现在有了
_computeModInverseButton = new JButton("Compute b-1 (mod a)");
Run Code Online (Sandbox Code Playgroud)
看起来像
如何格式化以适应上标?
我正在尝试使用附加到每个按钮的图标来构建匹配游戏.虽然这还没有完成,但我在用按钮填充面板时遇到了问题.
使用此代码,我得到一个灰色的框架.如果我注释掉我在"// execution"下使用的3种方法,那么面板将全部为黑色(这就是我如何测试按钮是否填充空间.)
对于某些原因,我的按钮没有填充到面板上.
我需要一些帮助!!!我哪里错了?
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MemoryMainFrame extends JFrame implements ActionListener {
JButton button[] = new JButton[16];
private static final int SIXTEEN_BUTTONS = 16;
JPanel mainPanel = new JPanel();
double dim = Math.sqrt(SIXTEEN_BUTTONS);
int numOfColumns = (int) (dim);
int numOfRows = (int) (dim);
public static void main(String[] args) {
new MemoryMainFrame();
}
public MemoryMainFrame() {
this.setTitle("MemoryGame!!!");
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true); …Run Code Online (Sandbox Code Playgroud) java ×10
jbutton ×10
swing ×10
button ×1
concurrency ×1
html ×1
jdialog ×1
jframe ×1
jpanel ×1
modal-dialog ×1
netbeans ×1
paint ×1
superscript ×1