尝试将JButton
的{{1}}控制器替换为JSpinner
。但由于某些原因,它不会听ImageIcon
中的任何mouseclicks(installButtonListeners()
似乎添加BasicSpinnerUI
)。任何想法为什么以及如何解决?
MouseListener
答案 0 :(得分:2)
不要更改Icon,请使用paintIcon()
如果移动了箭头按钮,则更改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();
}
});
}
}
答案 1 :(得分:1)
没有设法用答案来解决它。通过删除所有没有箭头的按钮调整按钮,然后我将箭头移动到一起:
private Component createArrowButton(int direction) {
JButton b = new BasicArrowButton(direction){
private static final long serialVersionUID = 1L;
@Override
public void paintTriangle(Graphics g, int x, int y, int size,
int direction, boolean isEnabled) {
if(direction == NORTH){
y +=3;
}
else if(direction == SOUTH){
y -=3;
}
Color shadow = UIManager.getColor("controlShadow");
Color darkShadow = UIManager.getColor("controlDkShadow");
Color highlight = UIManager.getColor("controlLtHighlight");
Color oldColor = g.getColor();
int mid, i, j;
j = 0;
size = Math.max(size, 2);
mid = (size / 2) - 1;
g.translate(x, y);
if (isEnabled)
g.setColor(darkShadow);
else
g.setColor(shadow);
switch (direction) {
case NORTH:
for (i = 0; i < size; i++) {
g.drawLine(mid - i, i, mid + i, i);
}
if (!isEnabled) {
g.setColor(highlight);
g.drawLine(mid - i + 2, i, mid + i, i);
}
break;
case SOUTH:
if (!isEnabled) {
g.translate(1, 1);
g.setColor(highlight);
for (i = size - 1; i >= 0; i--) {
g.drawLine(mid - i, j, mid + i, j);
j++;
}
g.translate(-1, -1);
g.setColor(shadow);
}
j = 0;
for (i = size - 1; i >= 0; i--) {
g.drawLine(mid - i, j, mid + i, j);
j++;
}
break;
case WEST:
for (i = 0; i < size; i++) {
g.drawLine(i, mid - i, i, mid + i);
}
if (!isEnabled) {
g.setColor(highlight);
g.drawLine(i, mid - i + 2, i, mid + i);
}
break;
case EAST:
if (!isEnabled) {
g.translate(1, 1);
g.setColor(highlight);
for (i = size - 1; i >= 0; i--) {
g.drawLine(j, mid - i, j, mid + i);
j++;
}
g.translate(-1, -1);
g.setColor(shadow);
}
j = 0;
for (i = size - 1; i >= 0; i--) {
g.drawLine(j, mid - i, j, mid + i);
j++;
}
break;
}
g.translate(-x, -y);
g.setColor(oldColor);
}
};
b.setBorder(BorderFactory.createEmptyBorder());
//removes content area
b.setContentAreaFilled(false);
b.setInheritsPopupMenu(true);
b.setOpaque(false);
b.setBackground(color);
return b;
}
答案 2 :(得分:0)
installNextButtonListeners()
和installPreviousButtonListeners()
调用以下方法:
private void installButtonListeners(Component c,
ArrowButtonHandler handler) {
if (c instanceof JButton) {
((JButton)c).addActionListener(handler);
}
c.addMouseListener(handler);
}
instanceof
检查ArrowButtonHandler
的原因不会ActionListener
作为JLabel
附加到处理转动的JButton
。
您可以使用JLabel
的子类代替{{1}}。