所以我有这个JButtons我添加了图标.最初的图标太大,所以我事先调整它们的大小,它工作正常.除了当我调整窗口大小时,JButton会改变大小,但不会改变图标,这是有问题的.
有没有办法让Icon只填充它附加的JButton?使代码更清晰的代码:
public JewelClass(){
setBackground (new Color (30,30,30));
addActionListener(this);
setLayout(new GridLayout());
ImageIcon icon = new ImageIcon(src/carre.jpg);
setIcon (resizeIcon(icon,60,60));
}
Run Code Online (Sandbox Code Playgroud)
resizeIcon是一个个人函数,它接受一个Icon,一个width参数和一个height参数,并返回一个调整大小的Icon(显然).我尝试更改布局,但它没有改变任何东西.我尝试获取JButton的宽度/高度,但是因为它们在添加Icon时尚不存在,所以它不起作用.
你们有没有想过如何解决这个问题?它不一定是一个图标,只要我的JButton充满我给它的图像,它真棒:)
谢谢!
使用NetBeans将ImageIcon添加到按钮属性.
print.setFont(new java.awt.Font("Serif", 0, 14));
print.setIcon(new javax.swing.ImageIcon(getClass().getResource("/project/print.gif")));
print.setMnemonic('P');
print.setText("Print");
print.setToolTipText("Print");
Run Code Online (Sandbox Code Playgroud)
编译时显示
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at project.Editor.initComponents(Editor.java:296)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我JPanel装了一个JFrame.JPanel包含4个JButtons.我想要做的是自动将焦点放在第一个jbutton上,这样我就可以在JButtons和键盘之间进行遍历.
我jButton1.requestFocusInWindow();在jpanel的构造函数中尝试了代码,但它仍然没有用.有什么东西我不见了吗?我还能做什么?
编辑:项目包含3个java文件,其代码如下:
NewJFrame.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sampleui;
import java.awt.Container;
import java.awt.Toolkit;
import javax.swing.JRootPane;
import javax.swing.UIManager;
/**
*
* @author Administrator
*/
public class NewJFrame extends javax.swing.JFrame {
static Container container;
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
rootPane= getRootPane();
container=getContentPane();
setSize(Toolkit.getDefaultToolkit().getScreenSize());
setVisible(true);
}
/**
* This method is called from within the …Run Code Online (Sandbox Code Playgroud) 作为我程序的一部分,我需要一个按钮,当用户点击它时,它会打开一个新窗口.
好吧,我想我应该有一个类来制作框架并通过按钮调用它.但我没有任何想法开始.我只是在程序中得到了我的按钮,但它不起作用.有人能告诉我怎么做吗?或编码.
我想将YES和NO改为同意/不同意.我该怎么办?
int reply = JOptionPane.showConfirmDialog(null,
"Are you want to continue the process?",
"YES?",
JOptionPane.YES_NO_OPTION);
Run Code Online (Sandbox Code Playgroud) 我有自己的Dialog弹出两个文本字段,两个JLabel和一个"ok"JButton.弹出窗口是一个登录窗口.窗口工作完美我只是想知道我如何能够添加"取消"JButton,因此用户可以取消登录.
这是我的窗口代码:
public Hashtable<String, String> login(JFrame frame) {
Hashtable<String, String> logininformation = new Hashtable<String, String>();
JPanel panel = new JPanel(new BorderLayout(5, 5));
JPanel label = new JPanel(new GridLayout(0, 1, 2, 2));
label.add(new JLabel("E-Mail", SwingConstants.RIGHT));
label.add(new JLabel("Password", SwingConstants.RIGHT));
panel.add(label, BorderLayout.WEST);
JPanel controls = new JPanel(new GridLayout(0, 1, 2, 2));
JTextField username = new JTextField();
controls.add(username);
JPasswordField password = new JPasswordField();
controls.add(password);
panel.add(controls, BorderLayout.CENTER);
JOptionPane.showMessageDialog(frame, panel, "login", JOptionPane.QUESTION_MESSAGE);
logininformation.put("user", username.getText());
logininformation.put("pass", new String(password.getPassword()));
return logininformation;
}
Run Code Online (Sandbox Code Playgroud)
如果需要,请点击登录窗口的屏幕截图:

如果你点击右下角的"x",它也会关闭.但我想要取消JButton,如果它很容易.
我用这个myButton.setBackground(myColor)来改变JButton背景颜色到我的颜色,如何找到它的原始默认背景颜色,以便我可以改回来?我知道在更改和使用它之前我可以保存它的默认背景颜色,但是我想知道Java是否将它存储在某处,以便我可以调用类似的东西:myButton.getClass.getDefaultBackground()将它取回?
JButtons考虑按空格键与单击JButton相同(假设JButton具有焦点,我在这里假设).有没有办法关闭这种行为,所以他们忽略按空格键?
另外,更一般地说,是否有一种技术可以做到这一点AbstractButtons?
如何在java中创建一个JButton,不可见但可点击?
button.setVisible(false);
Run Code Online (Sandbox Code Playgroud)
使按钮不可见,但不可点击,是否有任何方法使其不可见,但可点击?
我试过做:
button.setVisible(false);
button.setEnabled(true);
Run Code Online (Sandbox Code Playgroud)
但那也不起作用.我想这样做是因为我想要一个带图像的按钮,如果我将不可见的JButton放在图像上,按钮会在你点击图像时响应,或者是隐形按钮.
我将我的按钮图标设置为我在photoshop中制作的.png,但不仅仅是图像可见,还有按钮边框或者您想要调用的内容.

我希望按钮只是:

jbutton ×10
java ×9
swing ×8
imageicon ×2
joptionpane ×2
background ×1
clickable ×1
colors ×1
default ×1
focus ×1
invisible ×1
jframe ×1
jpanel ×1
jtextfield ×1
netbeans-7 ×1
resize ×1