String to button java

Java JButton setText(String text) Sets the button’s text.

The method setText() from JButton is declared as:

@BeanProperty(preferred = true, visualUpdate = true, description = "The button's text.") public void setText(String text) 

The method setText() has the following parameter:

Example

The following code shows how to use JButton from javax.swing.

Specifically, the code shows you how to use Java JButton setText(String text)

/**// ww w . de m o 2 s. c o m * @author Knut Hartmann * Flensburg University of Applied Sciences * Knut.Hartmann@FH-Flensburg.DE * * @version October 19, 2012 */ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; @SuppressWarnings("serial") public class ActionListenerDemo extends JFrameDemo implements ActionListener < public ActionListenerDemo() < initCanvas("ActionListener Demo", 800, 600); // insert a button into the display container JButton btn = new JButton("Du triffst mich NIE!"); btn.addActionListener(this); add(btn); setVisible(true); > public void actionPerformed(ActionEvent e) < JButton btn = (JButton) e.getSource(); btn.setText("Das gildet nicht!"); > public static void main(String[] args) < new ActionListenerDemo(); > >
import java.awt.BorderLayout; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.name.Named; public class TestFrame extends JFrame < @Inject// w w w . d em o 2 s . c o m public TestFrame(Panelik panel, @Named("nazwa") JButton button, Sluchacz s, @Named("quitaction") AbstractAction asquit, JButton quit) < super(); add(panel, BorderLayout.CENTER); JPanel p = new JPanel(); button.addActionListener(s); //guzik.addActionListener(s); // button.setText("Przycisk 2"); p.add(button); //p.add(guzik); quit.addActionListener(asquit); quit.setText("Quit"); p.add(quit); add(p, BorderLayout.NORTH); pack(); setVisible(true); > public static void main(String args[]) < Injector injector = Guice.createInjector(new TestFrameModule()); TestFrame tf = injector.getInstance(TestFrame.class); > >
import delautil.DeclayGridBagLayoutAdapter; import delautil.DeclayTemplateParser; import delautil.TemplateMatrix; import javax.swing.*; import java.util.HashMap; import java.util.Map; /**// w w w . d em o 2 s . c o m * Created by mattia on 04/03/14. */ public class GridBagLayoutExample extends JPanel < JButton[] buttons = new JButton[10]; String str1 = " < 1>(#) | (#) | | (#) \n" + " < 4>(#) : | (#) | \n" + " < 9>(#) : | | \n" + " ^ | | (#) \n" + " < 6>__200__ | .> ^1^ [RB100] : | ^ "; // weightx ^1^ weighty public GridBagLayoutExample() < MapString, JComponent> map = new HashMapString, JComponent>(); for (int i = 0; i < buttons.length; i++) < JButton button = new JButton(); button.setText(String.valueOf(i)); buttons[i] = button; map.put(String.valueOf(i), button); > DeclayTemplateParser parser = new DeclayTemplateParser(); TemplateMatrix gridComponents = parser.createComponentMatrix(str1); DeclayGridBagLayoutAdapter adapter = new DeclayGridBagLayoutAdapter(gridComponents); adapter.addLayoutAndComponents(this, map); > public static void main(String[] args) < JFrame frame = new JFrame(); GridBagLayoutExample panel = new GridBagLayoutExample(); frame.setContentPane(panel); frame.pack(); frame.setSize(frame.getWidth() + 100, frame.getHeight() + 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); > >
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.filechooser.FileSystemView; import control.Dovah; public class FrmMain extends JFrame < private static final long serialVersionUID = 7116845702666896682L; public FrmMain() < setLayout(null);// w w w . de m o 2 s. c o m setSize(510, 560); setLocation(100, 100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Dovah"); final JTextArea txtTexto = new JTextArea(); txtTexto.setSize(480, 480); txtTexto.setLocation(10, 10); JButton btnTraduzir = new JButton(); btnTraduzir.setText("Traduzir"); btnTraduzir.setSize(200, 30); btnTraduzir.setLocation(10, 490); btnTraduzir.addActionListener(new ActionListener() < @Override public void actionPerformed(ActionEvent arg0) < FileSystemView filesys = FileSystemView.getFileSystemView(); // File[] roots = filesys.getRoots(); Dovah.translate(txtTexto.getText(), filesys.getHomeDirectory().getPath()); > >); add(txtTexto); add(btnTraduzir); setVisible(true); > public static void main(String[] args) < new FrmMain(); > >
import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class ActionListenerDemo extends JFrame < public ActionListenerDemo() < init();// w w w . d em o 2 s . co m this.setTitle("listener Test"); this.setSize(new Dimension(300, 330)); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setVisible(true); // TODO Auto-generated constructor stub > private void init() < JPanel panel = new JPanel(); JButton button = new JButton("button"); button.addActionListener(new ActionListener() < @Override public void actionPerformed(ActionEvent e) < Object obj = e.getSource(); JButton button2 = (JButton) obj; if (button2.getText().equals("button")) < button2.setText("close"); > else < button2.setText("button"); > > >); panel.add(button); this.add(panel); > public static void main(String[] args) < // TODO Auto-generated method stub new ActionListenerDemo(); > >
import java.io.File; import java.math.BigDecimal; import javax.swing.JButton; import com.triselstudios.nclicker.Item; import com.triselstudios.nclicker.Listener; import com.triselstudios.nclicker.nClicker; public class nClickerDesktop implements Listener < private final nClicker main; private final GUI gui; public nClickerDesktop() < main = new nClicker(this); gui = new GUI(main); Runtime.getRuntime().addShutdownHook(new Thread() < public void run() < main.save();/* w w w. d e m o2 s . c o m*/ > >); main.startGame(); > public static void main(String[] args) < new nClickerDesktop(); > public void log(String message) < gui.getTextArea().append(message + "\n"); gui.getTextArea().setCaretPosition(gui.getTextArea().getDocument().getLength()); > public void updateStats(BigDecimal niccholaspages, BigDecimal niccholaspagesPerSecond) < gui.setStats( niccholaspages + " niccholaspages (" + niccholaspagesPerSecond + " niccholaspages per second)"); > public void updateItem(Item item) < for (JButton button : gui.getItemButtons().keySet()) < if (gui.getItemButtons().get(button) == item) < button.setText( item.getName() + " (" + item.getCost() + ") - " + main.getInventory().getCount(item)); > > > public File getSaveFile() < return new File("data.dat"); > >

  • Java JButton getText() Returns the button’s text.
  • Java JButton setActionCommand(String actionCommand) Sets the action command for this button.
  • Java JButton setEnabled(boolean b) Enables (or disables) the button.
  • Java JButton setText(String text) Sets the button’s text.
  • Java JButton doClick() Programmatically perform a «click».
  • Java JButton setBackground(Color bg) Sets the background color of this component.
  • Java JButton setAlignmentX(float alignmentX) Sets the horizontal alignment.

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

String to button java

An implementation of a «push» button. Buttons can be configured, and to some degree controlled, by Action s. Using an Action with a button has many benefits beyond directly configuring a button. Refer to Swing Components Supporting Action for more details, and you can find more information in How to Use Actions, a section in The Java Tutorial. See How to Use Buttons, Check Boxes, and Radio Buttons in The Java Tutorial for information and examples of using buttons. Warning: Swing is not thread safe. For more information see Swing’s Threading Policy. Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans™ has been added to the java.beans package. Please see XMLEncoder .

Nested Class Summary

Nested classes/interfaces inherited from class javax.swing.AbstractButton

Nested classes/interfaces inherited from class javax.swing.JComponent

Nested classes/interfaces inherited from class java.awt.Container

Nested classes/interfaces inherited from class java.awt.Component

Field Summary

Fields inherited from class javax.swing.AbstractButton

Fields inherited from class javax.swing.JComponent

Fields inherited from class java.awt.Component

Fields inherited from interface javax.swing.SwingConstants

Fields inherited from interface java.awt.image.ImageObserver

Constructor Summary

Method Summary

Gets the value of the defaultButton property, which if true means that this button is the current default button for its JRootPane .

Overrides JComponent.removeNotify to check if this button is currently set as the default button on the RootPane , and if so, sets the RootPane ‘s default button to null to ensure the RootPane doesn’t hold onto an invalid button reference.

Sets the defaultCapable property, which determines whether this button can be made the default button for its root pane.

Источник

Class Button

This class creates a labeled button. The application can cause some action to happen when the button is pushed. This image depicts three views of a » Quit » button as it appears under the Solaris operating system:

The first view shows the button as it appears normally. The second view shows the button when it has input focus. Its outline is darkened to let the user know that it is an active object. The third view shows the button when the user clicks the mouse over the button, and thus requests that an action be performed.

The gesture of clicking on a button with the mouse is associated with one instance of ActionEvent , which is sent out when the mouse is both pressed and released over the button. If an application is interested in knowing when the button has been pressed but not released, as a separate gesture, it can specialize processMouseEvent , or it can register itself as a listener for mouse events by calling addMouseListener . Both of these methods are defined by Component , the abstract superclass of all components.

When a button is pressed and released, AWT sends an instance of ActionEvent to the button, by calling processEvent on the button. The button’s processEvent method receives all events for the button; it passes an action event along by calling its own processActionEvent method. The latter method passes the action event on to any action listeners that have registered an interest in action events generated by this button.

If an application wants to perform some action based on a button being pressed and released, it should implement ActionListener and register the new listener to receive events from this button, by calling the button’s addActionListener method. The application can make use of the button’s action command as a messaging protocol.

Источник

Читайте также:  Php for mail form
Оцените статью