import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.imageio.*;
import java.lang.*;
import java.io.*;
import javax.swing.*;
public class MainClass extends Component{
private Image bg;
private ImageIcon newgame;
private ImageIcon quit;
private ImageIcon options;
private JButton bquit;
private JButton boptions;
private JButton bnewgame;
private static Container pane; //Container
public void loadImage() {
try {
bg=ImageIO.read(new File("bg1.png"));
} catch (Exception e) {
}
if(bg!=null)
repaint();
}
public void paint(Graphics g) {
g.drawImage(bg,0,0,null);
}
public void imageButtons(JFrame f) {
try {
quit= new ImageIcon("quit.png");
options=new …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,当点击一个按钮时,它会变成蓝色.我用以下方法完成了这个:
button.setBackground(Color.BLUE)
Run Code Online (Sandbox Code Playgroud)
但是,当它第二次点击时,我想将它改回原来的"金属"外观.我用谷歌搜索和谷歌搜索,无法弄清楚如何在任何地方这样做.你如何将JButton改回原来的"金属"颜色?
假设您在NxN网格中有一个JButtons的GridLayout,代码如下:
JPanel bPanel = new JPanel();
bPanel.setLayout(new GridLayout(N, N, 10, 10));
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
{
JButton b = new JButton("(" + row + ", " + col + ")");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
bPanel.add(b);
}
}
Run Code Online (Sandbox Code Playgroud)
如何在网格中单独访问每个按钮以通过setText()更改按钮的名称?这需要在实际按下相关按钮之外完成.
因为每个按钮在本地实例化为"b",所以当前不可能为每个按钮提供全局可访问的名称.如何独立访问每个按钮?像JButton [] []这样的数组是否可以保存对所有按钮的引用?如何在上面的代码中设置?
任何输入都表示赞赏.
谢谢.
一直未能找到明确的答案.我知道一个按钮可以有多个监听器,但反过来呢?目前有一个问题,我在ButtonListener上设置的第二个按钮没有响应,我想知道这是否是原因.如果我想要做的不可能,你如何设置另一个ButtonListener?
与往常一样,任何帮助表示赞赏.
相关来源:
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import java.text.NumberFormat;
public class ClientApp extends JFrame
{
public static void main(String[] args)
{
new ClientApp();
}
//Declarations so they have scope outside of ClientApp()
private JButton switchCard;
private JPanel infoPanel;
private JPanel mainPanel;
private JPanel cartPanel;
private JPanel orderingPanel;
private JList candyList;
private CardLayout cl = new CardLayout();
private CardLayout cl2 = new CardLayout();
private JPanel …Run Code Online (Sandbox Code Playgroud) 我想连接方法,returnedConversion以便ActionListener在用户选择转换为和来自哪个临时比例之后将结果返回给.我知道代码有点脱节,至少可以说,所以请忽略所有注释掉的区域(除非你能指出我应该注意的区域).
如何将该方法returnedConversion中的代码连接到代码才能ActionListener正常运行?我是否正确地将JTextField盒子中的输入转换为双倍然后适当地将其转换回a String以将其传递回第二个JTextField盒子?
package temperatureConverter;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TempConverter extends JFrame {
private JComboBox firstComboBox;
private JComboBox secondComboBox;
private JTextField initialTemp;
private JTextField convertedTemp;
private JButton convertButton;
// private enum TempType { FAHRENHEIT, CELSIUS, KELVIN};
private static final String[] tempType = { "Fahrenheit", "Celsius",
"Kelvin" };
public static final String theInitialTempType = null;
public …Run Code Online (Sandbox Code Playgroud) 以下方法有什么区别:
jButton.getModel().isArmed()
jButton.getModel().isSelected()
jButton.getModel().isPressed()
Run Code Online (Sandbox Code Playgroud)
我不明白文档说的是什么,isArmed其余两个有明显的文档.但我不知道他们的表现如何.
我目前正在研究Java GUI实现,对于我的下一个任务,我们将创建一个程序来模拟房屋的控制.我们已经获得了非常自由的统治来构建它,但是我们喜欢它(只要它在Java中).我已经制定了这个模型:http://i.imgur.com/9RtWL7b.jpg(如下所示),这就是我想要制作的东西.
程序样机:

下面是我目前的代码,它产生了这个http://i.imgur.com/XZLiwWx.jpg(下面)
示例代码如下:

我的问题是; 如何让三个按钮位于屏幕左侧?我是以错误的方式来做这件事的吗?我觉得我下面的代码非常笨重而且没有组织,我应该采取不同的方法吗?
如果需要更多信息,请与我们联系.
import java.awt.*;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
public class ImageTest {
public static void main(String[] args) {
ImagePanel panel = new ImagePanel("program/assets/main_bg.jpg");
TopTabButton buttonHome = new TopTabButton("home");
TopTabButton buttonSettings = new TopTabButton("settings");
TopTabButton buttonHelp = new TopTabButton("help");
panel.add(buttonHome);
panel.add(buttonSettings);
panel.add(buttonHelp);
panel.setPreferredSize(new Dimension(1000, 760));
JFrame frame = new JFrame();
frame.setBackground(new Color(53, 56, 64));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setResizable(false); …Run Code Online (Sandbox Code Playgroud) 我的Actionlistener有一个小问题,当我点击按钮没有任何反应?我没有看到问题所在,所以另一双眼睛可以帮助我:)
public class GameOptions extends JPanel implements ActionListener{
public GameOptions(){
System.out.println("GameOptions Class test blabla");
easyButton().addActionListener(this);
mediumButton().addActionListener(this);
hardButton().addActionListener(this);
JPanel center = new JPanel(new GridLayout(4,1,10,10));
center.add(new JLabel("Chose Difficulty Level"));
center.add(easyButton());
center.add(mediumButton());
center.add(hardButton());
this.add(center, BorderLayout.CENTER);
this.setPreferredSize(this.getPreferredSize());
this.setFocusable(true);
this.requestFocusInWindow();
}
private JButton easyButton(){
JButton levelEasy = new JButton("Easy");
return levelEasy;
}
private JButton mediumButton(){
JButton levelMedium = new JButton("Medium");
return levelMedium;
}
private JButton hardButton(){
JButton levelHard = new JButton("Hard");
return levelHard;
}
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if(src …Run Code Online (Sandbox Code Playgroud) 我正在使用swing组件在Java中创建一个文本字段。我想使搜索文本字段出现在Mozilla或其他浏览器中。
我在文本字段中添加了一个按钮。我已设定的边框布局JTextField。一切工作正常,但是只要在文本字段中写入大文本(达到文本字段的给定大小),它就会在按钮后面。就像大家都已经看到的那样,这不会在搜索栏中出现。文本不能在按钮后面,而按钮和文本之间必须有一定的间隙。
有谁知道这是怎么做到的吗?
对于我的项目,我需要创建可自定义的按钮.除了我有问题,我不知道如何解决它.我有一个背景图像为我的按钮,我希望它可以在X和Y可扩展,根据文本将会结束而不会损失质量.你有什么想法?

java ×10
jbutton ×10
swing ×10
colors ×1
grid-layout ×1
image ×1
jpanel ×1
jtextfield ×1
mouseevent ×1