我正在编写一个简单的java程序.它需要三个参数.包含图像的文件夹,所需的输出文件夹和图像水印文件.程序将输入文件夹中每个图像的水印图像复制到输出文件夹中.
我正在写一个简单的GUI.用户介绍三个参数并按下按钮.该程序有效,但我想向用户提供反馈.我希望每次处理图像时都能更新JLabel字段.
在我的WatermarkFolders类中,我使用此代码段向用户显示一些反馈:
Gui.FEEDBACK.setText(text);
Run Code Online (Sandbox Code Playgroud)
问题是每次处理图像时JLabel都不会更新.它只是在处理完成后用100%完整输出进行更新.是否有一种简单的方法来更新这个异步.谢谢你的回答.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class Gui extends JPanel {
JButton button;
JFileChooser chooser;
JLabel inputLabel = new JLabel("Directorio donde estan las imagenes");
JTextField inputField = new JTextField(15);
JLabel outputLabel = new JLabel("Directorio donde guardar las copias con marca de agua");
JTextField outputField = new JTextField(15);
JLabel watermarkLabel = new JLabel("Archivo marca de agua (gif transparente)");
JTextField watermarkField = new JTextField(15);
public static JLabel FEEDBACK = new JLabel("Número de Imágenes: "); …Run Code Online (Sandbox Code Playgroud) 我有一个程序,它使用一个开关,按钮点击时传递给它的动作事件:
public void buttonImageReveal(ActionEvent e){
String temp = e.getActionCommand();
switch(temp){
case "1":
((JButton)e.getSource()).setIcon(one);
delay();
((JButton)e.getSource()).setIcon(null);
break;
Run Code Online (Sandbox Code Playgroud)
延迟只是对等待1秒的函数的调用:
public void delay(){
try
{
Thread.sleep(1000);
}
catch(InterruptedException e1)
{
e1.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
所有结果都是等待而没有图像,期望的结果是图像闪烁一秒钟.
提前致谢!
我的项目有点麻烦.
我有一个类,它是一个显示4个按钮的面板.
我有4个类(目前只有一个)是显示各种组件的面板.这些面板占据一个空间,一次只能看到一个面板.
我想要做的是用各自的按钮控制面板的可见性.例如,当用户点击button1时,应该出现panel1,当按下button2时,会出现panel2等.
这是我第一次使用java中的GUI,感谢任何帮助.
当我尝试通过转到其属性然后图标在Jbutton中添加背景时,它然后设置按钮的背景但是我在按钮上写的文本,它向右移动(在按钮区域之外).什么是这个解决方案?
blueButton.addActionListener(new blueButtonListner());
Run Code Online (Sandbox Code Playgroud)
输入此代码后会发生什么?
我认为Java编译器创建一个被调用的对象blueButtonListner(),它成为(参数)的输入addActionListener
如果这是正确的,因为我猜,那么这段代码也应该工作:
redButton.addActionListener(rr);
redButtonListener rr =new redButtonListener();
Run Code Online (Sandbox Code Playgroud)
但它显示错误.谁可以给我解释一下这个?
好的,我知道之前已经问过这个问题,但我似乎无法让我的应用程序在其他人的帖子的帮助下工作.
我只是想制作一个带有我制作的图片的屏幕,然后是3个按钮(就像游戏菜单一样).我已完成图像,我现在正试图制作这样的3个按钮.

我的代码目前是这样的:
public class Test {
static int WIDTH = 700;
static int HEIGHT = 600;
public static void main(String[] args) {
JLabel label = new JLabel();
JFrame f = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton("hello again");
button.addActionListener ((ActionListener) new Action1());
label.setIcon(new ImageIcon("C:\\Users\\barney\\workspace\\game\\res\\background.jpg"));
f.add(panel);
f.add(label);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setSize(WIDTH, HEIGHT);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setTitle("2d platformer v1.1");
f.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
那么我将如何添加图片中的按钮
也可以告诉我,如果我把我的代码设置错了,因为我是java的新手.
谢谢.
在我的代码中,我的状态okButton很糟糕,如此大而长,如何解决这个问题呢?
public class d7Table extends JFrame {
public JTable table;
public JButton okButton;
public d7Table() {
table = new JTable(myTableModel(res));
okButton = new JButton("Ok");
add(new JScrollPane(table), BorderLayout.CENTER);
add(okButton, BorderLayout.PAGE_END);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
this.setLocation(300, 60);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new d7Table();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
我删除了不相关的代码.

我想在java应用程序中将图像加载到JButton,所以当我单击按钮显示图像时,再次单击它以隐藏图像.我不希望将图像加载到标签上但是按钮.
我有一个gui服务器,首先调用joptionpanel输入端口号.如果我单击OK按钮,它看起来运行良好.但是如果在第一次运行中我单击取消,然后单击确定我的GUI冻结.我觉得上课start()让它冻结了.
对不起,这是一个重复的问题,但也许问题与其他问题不同.
这是我的服务器代码.任何人都可以帮助我.是什么导致gui冻结?
public class server {
// Main meathod
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
server server2 = new server(PORT);
server2.start();
}
public server(int PORT)
{
sdf = new SimpleDateFormat("[HH:mm:ss]");
go();
}
public void go() {
//The GUI hidden`enter code here`...
connect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
// When Exit button is pressed
if (ae.getSource() == connect) {
start();
}
}
});
}
// …Run Code Online (Sandbox Code Playgroud) 我JButton在界面上遇到问题,当按下按钮时触发此事件监听器:
if (event.getSource() == goButton)
{
MainLauncher.doProgram(assetsField.getText(), FileFinder.completePath(String.format("%s\\%s", modDirectoryPath, modList.getSelectedValue())), databaseField.getText());
}
Run Code Online (Sandbox Code Playgroud)
它运行以下代码:
public static void doProgram(final String baseGamePath, final String modPath, final String databasePath)
{
new Thread()
{
public void run()
{
System.out.println("Running: " + modPath + "\n");
reader = new MetaDataReader(databasePath);
reader.formConnection();
long start = System.currentTimeMillis();
long temp;
File cache = new File("RegisterCache.SC");
if (!cache.exists())
{
temp = System.currentTimeMillis();
start += System.currentTimeMillis() - temp;
System.out.println("Calculating number of files");
FileFinder baseGame = new FileFinder();
baseGame.calculateNumFiles(baseGamePath);
System.out.println(String.format("Loading %s …Run Code Online (Sandbox Code Playgroud)