我希望在JTextField中有一个JButton(带有一个文件夹图标图像),就像在JTextField的最右边一样,这样当单击时,该按钮会打开一个JFileChooser,当选择一个文件时,该路径为该文件出现在JTextField中.
我已经制作了这段代码,但没有显示出来.
public class TextFieldChooser extends JTextField {
public ImageIcon folderIcon;
public JButton btnFolder;
public TextFieldChooser(int columns) {
super(columns);
btnFolder = new JButton();
folderIcon = new ImageIcon(getClass().getResource("/resources/folder_find.png"));
btnFolder.setIcon(folderIcon);
this.add(btnFolder);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个边框布局,有4个面板,北,南,东,西.例如在东侧我有一个jpanel,它有四个按钮.我的问题是所有按钮都对齐到顶部,我想对齐中心.在css中,例如保证金顶部或顶部:50%.
有任何想法吗?
JPanel contentor3 = new JPanel();
contentor.add(contentor3, BorderLayout.EAST);
contentor3.setBackground(Color.green);
contentor3.setPreferredSize(new Dimension(120, 750));
contentor3.add(btn[1]);
btn[1].setText("btn1");
Run Code Online (Sandbox Code Playgroud) 我想用这些目标制作一个程序:
1)创建JButton 2)使用KeyBindings将按钮附加到键("A"键)3)单击"A"时执行一些代码
这是我到目前为止的代码:
// Imports
Public class Test{
JButton button = new JButton();
//...
Test(){
button.getInputMap().put(KeyStroke.getKeyStroke("A"), "Pressed");
//...
}
// Where do I add the code that responds when button is pressed?
}
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) 请帮助我了解如何让我的JButton多次工作?
截至目前我只能在每次点击按钮时替换面板一次,但是需要这样做:当用户按btnNext我要更改面板,然后在另一个btnNext按下更改面板后,同样适用于btnBack.
谢谢.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
public class beta1 extends JFrame implements ItemListener, ActionListener {
private final JButton btnNext, btnBack;
private JButton btnNext2;
private JButton btnAbout;
private final JRadioButton rb1, rb2, rb3, rb4, rb5, rb6;
private final JCheckBox chk1, chk2, chk3, chk4, chk5, chk6, chk7, …Run Code Online (Sandbox Code Playgroud) 每当我为我的JButton设置一个图标时,它的大小总是不正确.如何调整图标大小以完全适合按钮?
final JButton btnSanic = new JButton();
Image img = icon.getImage();
Image newimg = img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(newimg);
btnSanic.setIcon(icon);
Run Code Online (Sandbox Code Playgroud) 我有一个JFrame与JTextFields,JComboBoxes和JLabels.我想只在窗口中的某些内容被更改(字段)时才使"UPDATE"按钮可单击.
我有一个快速的问题.我想让我的代码更短,我想知道我是否可以在复选框下面以某种方式进入循环.这部分代码的意义在于,如果选中至少一个复选框,则启用"查找"按钮.提前感谢您的每一个提示.
if (checkBoxes[0].isSelected() == true || checkBoxes[1].isSelected() == true
|| checkBoxes[2].isSelected() == true || checkBoxes[3].isSelected() == true || checkBoxes[4].isSelected() == true
|| checkBoxes[5].isSelected() == true || checkBoxes[6].isSelected() == true || checkBoxes[7].isSelected() == true
|| checkBoxes[8].isSelected() == true || checkBoxes[9].isSelected() == true || checkBoxes[10].isSelected() == true
|| checkBoxes[11].isSelected() == true || checkBoxes[12].isSelected() == true || checkBoxes[13].isSelected() == true
|| checkBoxes[14].isSelected() == true || checkBoxes[15].isSelected() == true || checkBoxes[16].isSelected() == true
|| checkBoxes[17].isSelected() == true || checkBoxes[18].isSelected() == true || checkBoxes[19].isSelected() == …Run Code Online (Sandbox Code Playgroud) java ×10
jbutton ×10
swing ×9
jframe ×2
center ×1
checkbox ×1
icons ×1
jfilechooser ×1
jpanel ×1
jtextarea ×1
jtextfield ×1
key-bindings ×1
loops ×1
tooltip ×1