jbutton

Why setPreferredSize does not change the size of the button?

做~自己de王妃 提交于 2019-12-20 02:12:09
问题 Here is the code: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class TestGrid { public static void main(String[] args) { JFrame frame = new JFrame("Colored Trails"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(4, 9)); panel.setMaximumSize(new Dimension(9*30-20,4*30)); JButton btn; for (int i=1; i<=4;

How to set the button color of a JButton (not background color)

此生再无相见时 提交于 2019-12-19 19:13:15
问题 I have a JButton that I would like to change the background color of to white. When using the Metal Look And Feel, I achieve the desired effect with setBackground : Unfortunately, the concept of "background color" is different when using the Windows LAF; the background color is the color drawn around the button: I would like to use the Windows LAF, but allow the button color of this JButton to be changed to white. How do I do this? 回答1: You'll have to decide if it's worth the effort, but you

New Line \n is not working in JButton.setText(“fnord\nfoo”) ; [duplicate]

混江龙づ霸主 提交于 2019-12-19 16:55:25
问题 This question already has answers here : Word Wrap in JButtons (4 answers) Closed 4 years ago . On a JButton, I want to list information on multiple lines. I tried \n as a new line character but it didn't work. The following code: JButton.setText("fnord\nfoo") ; will be displayed as: fnordfoo How do I force a line break? 回答1: JButton accepts HTML, so for the line break to work use: JButton.setText("<html>fnord<br />foo</html>"); 来源: https://stackoverflow.com/questions/13503280/new-line-n-is

Showing/hiding a JPopupMenu from a JButton; FocusListener not working?

点点圈 提交于 2019-12-19 16:34:38
问题 I needed a JButton with an attached dropdown style menu. So I took a JPopupMenu and attached it to the JButton in the way you can see in the code below. What it needs to do is this: show the popup when clicked hide it if clicked a second time hide it if an item is selected in the popup hide it if the user clicks somewhere else in the screen These 4 things work, but because of the boolean flag I'm using, if the user clicks somewhere else or selects an item, I have to click twice on the button

How do you simulate a click on a JTextField? Equivalent of JButton doClick()?

三世轮回 提交于 2019-12-19 12:11:57
问题 I am working on a Java project and need to have a keypress simulate a click on a JTextField. What I'm looking for is the equivalent of the JButton doClick() method. I am trying to have the keypress "enter" perform the exact same function as a click on the JTextField. Not sure what other info to provide. Thanks in advance. 回答1: public void simulateKey(KeyEvent e, Component c) { Field f = KeyEvent.class.getField("focusManagerIsDispatching"); f.setAccessible(true); f.set(e, Boolean.TRUE); c

Multiple Jtextfields to be filled before Jbutton enable

女生的网名这么多〃 提交于 2019-12-19 11:26:48
问题 Hi I badly need some help I already search about Jtextfield to be filled before jbutton enables, DocumentListener most people use to determined if Jtextfield is being populated. I tried DocumentListener and it works but all I want is all Jtextfield must be not empty before the Jbutton enables here is my code. Ftext.getDocument().addDocumentListener(new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { change(); } @Override public void removeUpdate(DocumentEvent e) {

Remove Arrows from Swing Scrollbar in JScrollPane

南楼画角 提交于 2019-12-19 09:57:08
问题 I would like to remove the scrollbar arrow buttons from a scrollbar in a JScrollPane. How would I do this? 回答1: class NoArrowScrollBarUI extends BasicScrollBarUI { protected JButton createZeroButton() { JButton button = new JButton("zero button"); Dimension zeroDim = new Dimension(0,0); button.setPreferredSize(zeroDim); button.setMinimumSize(zeroDim); button.setMaximumSize(zeroDim); return button; } @Override protected JButton createDecreaseButton(int orientation) { return createZeroButton();

Change Default Buttons in Java to Make Them Look “Better”

大憨熊 提交于 2019-12-19 05:45:17
问题 I'm essentially trying to mimic the default windows xp simple calculator. When I change the background colours of the buttons in Java it makes them look very flat and "boring". I want to make the buttons look as close as possible to the buttons in the Windows XP calculator. Here is an image comparing mine to WinXp's: Is there some kind of method I can use to change the style of the buttons much like you can do in Visual Basic to make the buttons almost pop more or look 3D like the Windows Xp

how to set color of Jbutton

大兔子大兔子 提交于 2019-12-19 03:21:48
问题 How can I set the color of a JButton ? I have tried this: button.setBackground(Color.red); but with no success. This just changes the color of button's border. I then tried to override paintComponents public void paintComponent(Graphics g) { g.setColor(Color.GREEN); g.fillRect(0, 0, getSize().width, getSize().height); } but now I don't see the text on the JButton 回答1: The best way to color your buttons is to use ImageIcons instead of text. You could use Gimp in order to design them. Make sure

Making a JButton invisible, but clickable?

依然范特西╮ 提交于 2019-12-19 02:57:10
问题 How do I make a JButton in java, invisible, but clickable? button.setVisible(false); makes the button invisible, but unclickable, is there any method that makes it invisible, but clickable? I tried doing: button.setVisible(false); button.setEnabled(true); but that didn't work either. I want to do this because I want to have a button with an image, if I put the invisible JButton over the image, the button will respond when you click the image, or invisible button. 回答1: I think you mean