gpt4 book ai didi

java - JSpinner 的 JButton 到 ImageIcon

转载 作者:行者123 更新时间:2023-11-29 05:43:33 25 4
gpt4 key购买 nike

尝试将 JSpinnerJButton Controller 替换为 ImageIcon。但出于某种原因,它不听任何 mouseclicks(installButtonListeners() in BasicSpinnerUI 似乎添加了 MouseListener)。任何想法为什么以及如何解决?

public class SpinnerIconBtn extends JFrame {

public SpinnerIconBtn(){
JSpinner spinner = new JSpinner();
spinner.setUI(new JSpinnerArrow());

this.add(spinner);
this.pack();
this.setVisible(true);
}

class JSpinnerArrow extends BasicSpinnerUI {

@Override
protected Component createNextButton() {
Component c = createArrowButton(SwingConstants.NORTH);
c.setName("Spinner.nextButton");
installNextButtonListeners(c);
return c;
}

@Override
protected Component createPreviousButton() {
Component c = createArrowButton(SwingConstants.SOUTH);
c.setName("Spinner.previousButton");
installPreviousButtonListeners(c);
return c;
}

private Component createArrowButton(int direction) {
String path = "/Users/tst.png";
JLabel icon = new JLabel(new ImageIcon(path));
return icon;
}
}

public static void main(String[] args) {
new SpinnerIconBtn();
}
}

最佳答案

  • 不要改变图标,使用 paintIcon()

  • 如果 ArrowsButtons 移动或图标太大,请更改 LayoutManager

.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

class Testing {
//int counter = 0;//proper setting

int counter = 7195;//testing hours 'tick over' correctly
JSpinner spinner = new JSpinner();
JTextField editor = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
java.text.DecimalFormat df = new java.text.DecimalFormat("00");

public void buildGUI() {
spinner.setUI(new EndlessHoursUI());
JFrame f = new JFrame();
f.getContentPane().add(spinner);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

class EndlessHoursUI extends javax.swing.plaf.basic.BasicSpinnerUI {

public EndlessHoursUI() {
setTime();
}

@Override
protected void installNextButtonListeners(Component c) {
}// do nothing

@Override
protected void installPreviousButtonListeners(Component c) {
}// do nothing

@Override
protected Component createNextButton() {
JButton btnNext = (JButton) super.createNextButton();
btnNext.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent ae) {
changeSpinner(1);
}
});
return btnNext;
}

@Override
protected Component createPreviousButton() {
JButton btnPrevious = (JButton) super.createPreviousButton();
btnPrevious.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent ae) {
changeSpinner(-1);
}
});
return btnPrevious;
}
}

public void changeSpinner(int amount) {
if (counter > 0 || amount > 0) {
counter += amount;
setTime();
}
}

public void setTime() {
int hours = counter / 3600;
int mins = (counter / 60) % 60;
int secs = counter % 60;
String time = df.format(hours) + ":" + df.format(mins) + ":" + df.format(secs);
editor.setText(time);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
new Testing().buildGUI();
}
});
}
}

关于java - JSpinner 的 JButton 到 ImageIcon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16643758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com