我目前正在读一本关于java的书,目前正在研究swing图形用户界面组件。当我这样做时,我偶然发现了一个代码示例,其中作者以一种非常不寻常的方式在 JButton 上设置图像,如下所示:
Icon bug1 = new ImageIcon( getClass().getResource( "bug1.gif" ) );
Run Code Online (Sandbox Code Playgroud)
为了使上述工作正常,您需要将图像与 .class 文件放在同一文件夹中。有人可以向我解释一下为什么他使用这个特定的代码(据我所知,它一定是反射代码,但话又说回来,我对此不是特别确定)以及是否有一种方法可以让我做到这一点同样的事情,而不会让事情像他那样复杂?
所以我的代码基本上有一个JPanel带有一些文本字段和一个JButton,当用户单击按钮时它会转到按钮侦听器,然后从文本字段中获取数据并处理它,创建JLabels它添加到另一个不可见的JPanel. 然后我让第一个 JPanel 不可见,让第二个面板可见,显示我生成的“结果”。
这一切都有效,但问题是,当我的程序处理它从文本字段获取的数据时,我希望JButton更改它所说的内容,并且我已经尝试使用event.getSource().setText(),并且我能够发现它正在更改按钮文本(通过打印到控制台),但它不会使用更改的文本更新按钮。
在此之后,我尝试了所有形式的重新验证、重新绘制和验证,但都没有奏效。有任何想法吗?谢谢!
//entryPanel is the first panel, and picksPanel is the second panel
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
((JButton)event.getSource()).setText("Thinking...");
revalidate();
repaint();
try
{
CriticPick picks = new CriticPick(cityfield.getText(),statefield.getText());
LinkedList<Movie> pickslist = picks.getList();
glayout.setRows(pickslist.size()+2+thepicks.movnum);
picksPanel.add(new JLabel("The Results:"));
//In my actual code I do a bunch of processing and looping that results in jlabels being added to picksPanel
for (int i=0;i<pickslist.size();i++)
{
JLabel …Run Code Online (Sandbox Code Playgroud) 我想JButton在用户单击时删除JButton。
我知道我应该使用 remove 方法,但它没有用。
我怎样才能做到这一点?
这是我的代码:
class Game implements ActionListener {
JFrame gameFrame;
JButton tmpButton;
JLabel tmpLabel1, tmpLabel2, tmpLabel3, tmpLabel4;
public void actionPerformed(ActionEvent e) {
gameFrame.remove(tmpLabel1);
gameFrame.getContentPane().validate();
return;
}
Game(String title) {
gameFrame = new JFrame(title);
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setBounds(100, 100, 300, 500);
gameFrame.setResizable(false);
gameFrame.getContentPane().setLayout(null);
tmpLabel4 = new JLabel(new ImageIcon("./images/bomber.jpg"));
tmpLabel4.setSize(200, 200);
tmpLabel4.setLocation(50, 100);
tmpButton = new JButton("Play");
tmpButton.setSize(100, 50);
tmpButton.setLocation(100, 350);
tmpButton.addActionListener(this);
gameFrame.getContentPane().add(tmpLabel4);
gameFrame.getContentPane().add(tmpButton);
gameFrame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud) 我编写了包含简单 JFrame 的类,我想将其他类的按钮添加到此 JFrame
当我尝试写作时
panel.add(new CancelButton())
Run Code Online (Sandbox Code Playgroud)
我收到错误:LoginWindow.java:29: 错误:没有找到适合 add(CancelButton) 的方法
你能帮我吗?
import javax.swing.*;
import java.awt.*;
public class LoginWindow {
public LoginWindow(){
//Login Window
JFrame frame = new JFrame("Movie date base");
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//label
JLabel label = new JLabel("Welcom to Movie Date Base! ");
label.setFont(new Font("Verdana", Font.PLAIN, 18));
//panel
JPanel panel = new JPanel(new GridBagLayout());
JPanel panel1 = new JPanel(new GridBagLayout());
//add panel to the frame
frame.add(panel);
frame.getContentPane().add(panel, BorderLayout.NORTH);
frame.getContentPane().add(panel1, BorderLayout.SOUTH);
//Grid layout
GridBagConstraints c = new GridBagConstraints();
c.insets …Run Code Online (Sandbox Code Playgroud) 我正在编写一个Java程序,其中有一系列按钮(不是计算器!),我正在寻找一种有效的方法来了解哪个JButton被点击了.据我所知,到目前为止,Java的唯一方法是将它们全部放在同一个ActionListener中,然后循环查找匹配项.
我刚想到的另一个解决方案是扩展JButton以在构造函数中包含唯一的ID号变量.在检查instanceof后将事件对象强制转换为JButton时,似乎应该可以工作.有点像使用分配给索引号的VB的Tag属性.
有更好/更优雅的方式吗?
我有一些代码如下:
public static void main(String[] args) {
JFrame f = new BorderTest();
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JButton("West"), "West");
p.add(new JButton("East"), "East");
p.add(new JButton("North"), "North");
p.add(new JButton("Center"), "Center");
p.add(new JButton("South"), "South");
Container c = f.getContentPane();
c.add(p);
f.pack();
f.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)
哪个产生:
我如何修改我的代码以使其按钮北和南的宽度与中心相同,西和东向垂直扩展以填充空间?
谢谢.
我有一个 jButton,我想在它上面分配一个快捷方式。就像当我按键盘上的删除键时,它只单击该 jButton 一次。我怎样才能做到这一点?
好吧,这是我想使用透明度时偶然发现的一个问题。
所以改变悬停背景的代码是这样的......
received.setMouseListener(new MouseAdapter()
@Override
public void mouseEntered(MouseEvent me)
{
received.setBackground(new Color(50,50,50,100));
}
});
Run Code Online (Sandbox Code Playgroud)
一开始我为按钮设置了蓝色。
这是显示颜色变化的 gif...
GifMeme09541718022016.gif https://drive.google.com/file/d/0B9XFyaTVy8oYci1zMmRhMmtYcnM/view?usp=docslist_api
为什么会出现这种情况?如果这不是正确的方法,那么正确的方法是什么?
我在 Java 上使用 Swing 进行字符编码时遇到问题。我想写这个角色:
"\u2699"
Run Code Online (Sandbox Code Playgroud)
这是一个简单的 JButton 上的齿轮,但是当我启动我的程序时,我只得到一个带有正方形的 JButton 而不是齿轮。这是一行:
opt.setText("\u2699");
Run Code Online (Sandbox Code Playgroud)
其中 opt 是按钮。
按钮结果:
我可以更改 Swing 字符编码或其他内容吗?谢谢。
我尝试过重新排列这一切,并使用不同的方法,这些方法不是静态的,但按钮从未出现。所有这些代码都是用于国际象棋的,但我正在努力添加按钮以使其可玩,并且在过去的几天里我一直在努力解决这个问题。还有其他问题,例如鼠标没有执行任何操作,但我只是删除了所有内容以尝试专注于按钮。A
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Main extends Canvas implements ActionListener, MouseListener {
private static JButton try144Button = new JButton();
public static void main(String[] args) {
JFrame frame = new JFrame("Chess");
Canvas canvas = new Main();
canvas.setSize(1000, 1000);
frame.add(canvas);
frame.add(try144Button);
frame.pack();
frame.setVisible(true);
}
public Main(){
try144Button.setBounds(0, 0, 500, 500);
try144Button.setText("CLICK ME");
try144Button.setBounds(210, 60, 150, 150);}
private void add(JButton try144Button) {
}
public void paint(Graphics g) {
int xshift;
int yshift = …Run Code Online (Sandbox Code Playgroud)