I try to change jtextfield border after 5 seconds. But doesnt work. My code:
// Here vaildate a field and set border to red
if (ApplicationNameField.getText().equals("")) {
Border newBorder = BorderFactory.createLineBorder(Color.RED, 1);
ApplicationNameField.setBorder(newBorder);
ErrorCode.setText("Error field cant be empty");
}
if (ApplicationHostField.getText().equals("")) {
Border newBorder = BorderFactory.createLineBorder(Color.RED, 1);
ApplicationHostField.setBorder(newBorder);
ErrorCode.setText("Error field cant be empty");
}
// here i would change border to start normal color (color black)
try {
TimeUnit.SECONDS.sleep(5);
Border newBorder = BorderFactory.createLineBorder(Color.BLACK, 1);
ApplicationNameField.setBorder(newBorder);
Border newBorder2 = BorderFactory.createLineBorder(Color.BLACK, 1);
ApplicationHostField.setBorder(newBorder2);
} catch (InterruptedException ex) {
System.out.print(ex.getMessage());
}
After this code my JTextFields borders are black and is not colored by red color.
Also i tried with:
Thread.sleep(5000);
But effect is the same. Anyone could help?
You need to use a javax.swing.Timer to trigger the desired action after 5 seconds. Thread.wait (or any other kind of waiting) doesn't work as expected because nothing is drawn until the thread returns to the main event loop.
Agree with #ammoQ. Just provide you an example of the use of javax.swing.Timer.
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.AbstractBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class TimerTest implements ActionListener{
JTextField textField;
public void createUI(){
JFrame frame = new JFrame("Timer Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
MainPanel mainPanel = new MainPanel();
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
frame.add(mainPanel,BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Timer timer = new Timer(5 * 1000, this);
timer.start();
}
public static void main(String[] args) {
TimerTest timerTest = new TimerTest();
timerTest.createUI();
}
#SuppressWarnings("serial")
class MainPanel extends JPanel{
public MainPanel(){
textField = new JTextField();
textField.setColumns(30);
textField.setBorder(BorderFactory.createCompoundBorder(
new LineBorder(Color.red, 3),
new EmptyBorder(new Insets(15, 25, 15, 25))));
add(textField,BorderLayout.CENTER);
setBackground(new Color(211,211,211));
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
}
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
textField.setBorder(BorderFactory.createCompoundBorder(
new LineBorder(Color.black, 3),
new EmptyBorder(new Insets(15, 25, 15, 25))));
}
}
you can use SwingWorker to do this. see my running example below.
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;
public class JTextFieldTest extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JTextFieldTest frame = new JTextFieldTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JTextFieldTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 298, 220);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
textField = new JTextField();
textField.setColumns(10);
textField.setBorder(BorderFactory.createLineBorder(Color.RED));
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
#Override
protected Void doInBackground() throws Exception {
// Simulate doing something useful.
Thread.sleep(5000);
textField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
return null;
}
};
worker.execute();
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(
Alignment.LEADING).addGroup(
gl_contentPane
.createSequentialGroup()
.addGap(87)
.addComponent(textField, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addContainerGap(99, Short.MAX_VALUE)));
gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(
Alignment.LEADING).addGroup(
gl_contentPane
.createSequentialGroup()
.addGap(70)
.addComponent(textField, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addContainerGap(81, Short.MAX_VALUE)));
contentPane.setLayout(gl_contentPane);
}
}
Related
I am currently trying to write a simple program that will display musical notes at a given interval in BPM. I have most of the code written, but I can't figure out how to get the labels containing the note and sharp/flat to update every interval given. This is my code so far. The NoteGenerator object is of my own creation and is just used to generate a random note/sharp/flat and keep track of tempo. I have already tested the NoteGenerator class and it works exactly how I expect it to. Thanks in advance!
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JSpinner;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.SpinnerNumberModel;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class NotePractice extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
NotePractice frame = new NotePractice();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public NotePractice() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
NoteGenerator noteGen = new NoteGenerator();
JSpinner spinner = new JSpinner();
spinner.setModel(new SpinnerNumberModel(new Integer(100), new Integer(20), null, new Integer(5)));
spinner.setBounds(211, 196, 60, 20);
contentPane.add(spinner);
JLabel lblBpm = new JLabel("BPM");
lblBpm.setFont(new Font("Arial", Font.BOLD, 12));
lblBpm.setBounds(175, 198, 26, 14);
contentPane.add(lblBpm);
JLabel lblNote = new JLabel(noteGen.getNote());
lblNote.setFont(new Font("Arial", Font.BOLD, 99));
lblNote.setBounds(175, 60, 82, 104);
contentPane.add(lblNote);
JLabel lblStep = new JLabel(noteGen.getStep());
lblStep.setFont(new Font("Arial", Font.BOLD, 70));
lblStep.setBounds(258, 93, 60, 58);
contentPane.add(lblStep);
JButton btnUpdateTempo = new JButton("Update Tempo");
btnUpdateTempo.setFont(new Font("Arial", Font.PLAIN, 11));
btnUpdateTempo.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
noteGen.setBPM(spinner.getComponentCount());
Timer t = new Timer(noteGen.getBPM(), new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
lblNote.setText(noteGen.getNote());
lblStep.setText(noteGen.getStep());
contentPane.updateUI();
}
});
}
});
btnUpdateTempo.setBounds(168, 227, 103, 23);
contentPane.add(btnUpdateTempo);
}
}
I'm having a problem with my About Frame class. I call it through an action listener in a JMenuItem. It comes up but it doesn't show it centered and the frame doesn't show the icon image as I request. I have the icon working in the mainframe so it's the image size isn't the problem. Does this have something to do with the private JPanel contentPane;?
Code in the JmenuItem:
aboutMItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jAboutMN_actionPerformed(e);
About Frame code:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
public class AboutFrame extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AboutFrame frame = new AboutFrame();
frame.setIconImage(new ImageIcon("resources/Image.png").getImage());
frame.setVisible(true);
//frame.setLocationRelativeTo(null);
frame.setLocationRelativeTo(null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public AboutFrame() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//AboutFrame.setLocationRelativeTo(null);
setTitle("About MyAPP");
setBounds(100, 100, 370, 250);
//contentPane.setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lbVerNum = new JLabel("MYAPP, Beta 1.2");
lbVerNum.setBounds(57, 11, 233, 19);
lbVerNum.setVerticalAlignment(SwingConstants.TOP);
lbVerNum.setFont(new Font("Tahoma", Font.BOLD, 15));
lbVerNum.setHorizontalAlignment(SwingConstants.CENTER);
contentPane.add(lbVerNum);
JLabel lbCopyright = new JLabel("Copyright 2015 ");
lbCopyright.setBounds(86, 41, 170, 16);
lbCopyright.setIcon(null);
lbCopyright.setFont(new Font("Tahoma", Font.PLAIN, 13));
lbCopyright.setVerticalAlignment(SwingConstants.TOP);
lbCopyright.setHorizontalAlignment(SwingConstants.CENTER);
contentPane.add(lbCopyright);
}
}
My application gives a grey screen after the timer is run. As advised, I have now a MainPage which extends JFrame and a MenuPage that extends JPanel. I wish to load MenuPage after MainPage is run. repaint() and revalidate() does not work out for me. Please point me in the right direction.
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
public class MainPage extends JFrame {
private static JPanel contentPane;
//timer
private final static int interval = 40;
private int i;
private Timer t;
private JProgressBar pbar;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainPage frame = new MainPage();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainPage() {
dim = Toolkit.getDefaultToolkit().getScreenSize();
System.out.println(dim);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
contentPane.setBounds(0,0,dim.width,dim.height);
setContentPane(contentPane);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
pbar = new JProgressBar (0,20);
pbar.setBounds(600, 500, 200, 45);
pbar.setValue(0);
pbar.setStringPainted(true);
pbar.setForeground(Color.RED);
Border border = BorderFactory.createTitledBorder("Loading...");
pbar.setBorder(border);
t = new Timer (interval, new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (i == 20){
t.stop();
//start.setEnabled(true);
//refresh + load next page
contentPane.removeAll();
MenuPage menuPage = new MenuPage();
//setContentPane(menuPage);
contentPane.add(menuPage);
contentPane.revalidate();
contentPane.repaint();
contentPane.setVisible(true);
}
else{
i++;
pbar.setValue(i);
}
}
});
t.start();
contentPane.add(pbar, BorderLayout.NORTH);
contentPane.add(lblTitle);
contentPane.add(imgLogo);
contentPane.add(imgBackground);
}
}
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MenuPage extends JPanel {
private JPanel contentPane;
public MenuPage() {
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setSize(500, 500);
contentPane.setLayout(null);
add (contentPane);
JButton btnSadfsafsa = new JButton("sadfsafsa");
btnSadfsafsa.setBounds(10, 52, 89, 23);
btnSadfsafsa.setEnabled(true);
btnSadfsafsa.setVisible(true);
contentPane.add(btnSadfsafsa);
}
}
Your MenuPage constructor is the problem.
You create a new JPanel - contentPane but never add it and never set the size. So in fact you just create an empty panel.
I hope this helps others in future. Yes null layout is not to be recommended. Applied setContentPane() instead of contentPane.add() in my case.
//refresh + load next page
contentPane.removeAll();
contentPane.revalidate();
contentPane.repaint();
setContentPane(new MenuPage());
Ive created two frames my main frame is Home and the second one is Selectie
On home there is a button which open the frame selectie, but i want when i click this button the main frame home wil dissapear and only selectie will be shown. The code for the button ive make in a other package and i dont want it in the same class as my main (home)
Code from Home:
package View;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import Controller.HomeController;
import music.PlaySound;
public class Home extends JFrame {
private JLabel label, label1, label2;
private JPanel panel;
private JButton logo, logo1, logo2, logo3, logo4, logo5, selectie;
private Container window = getContentPane();
private HomeController Controller;
public Home (){
initGUI();
Controller = new HomeController();
}
public void addHomeListener(ActionListener a){
selectie.addActionListener(a);
}
public void initGUI(){
setLayout(null);
setTitle("");
setPreferredSize(new Dimension(800,600));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel();
label.setBounds(0, 0, 266, 800);
label.setBackground(Color.WHITE);
label.setOpaque(true);
window.add(label);
label1 = new JLabel();
label1.setBounds(267, 0, 266, 800);
label1.setBackground(Color.RED);
label1.setOpaque(true);
window.add(label1);
label2 = new JLabel();
label2.setBounds(533, 0, 266, 800);
label2.setBackground(Color.WHITE);
label2.setOpaque(true);
window.add(label2);
logo = new JButton(new ImageIcon("../Ajax/src/img/logotje.gif"));
logo.setBorderPainted(false);
logo.setBounds(40, 150, 188, 188);
label1.add(logo);
logo1 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
logo1.setBorderPainted(false);
logo1.setBounds(10, 50, 82, 82);
label1.add(logo1);
logo2 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
logo2.setBorderPainted(false);
logo2.setBounds(92, 20, 82, 82);
label1.add(logo2);
logo3 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
logo3.setBorderPainted(false);
logo3.setBounds(174, 50, 82, 82);
label1.add(logo3);
logo4 = new JButton(new ImageIcon("../Ajax/src/img/shirt.png"));
logo4.setBorderPainted(false);
logo4.setBounds(50, 50, 135, 182);
label.add(logo4);
logo5 = new JButton(new ImageIcon("../Ajax/src/img/uitshirt.png"));
logo5.setBorderPainted(false);
logo5.setBounds(65, 50, 138, 190);
label2.add(logo5);
selectie = new JButton("Selectie");
selectie.setBounds(60, 500, 99, 25);
selectie.setActionCommand("selectie");
label.add(selectie);
pack();
addHomeListener(new HomeController());
}
}
Code from the button:
package Controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import View.Home;
import View.Selectie;
public class HomeController implements ActionListener {
public void actionPerformed (ActionEvent e){
Selectie selectie = new Selectie();
selectie.setVisible(true);
}
}
Please do give valid attention to what #kleopatra and #mKorbel, has to say, they are very much right in pointing that out to you to make things easier.
Here I had added some comments in the code, do check this out :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.File;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
//import Controller.HomeController;
//import music.PlaySound;
public class Home extends JFrame {
private JLabel label, label1, label2;
private JPanel panel;
private JButton logo, logo1, logo2, logo3, logo4, logo5, selectie;
private Container window = getContentPane();
private HomeController Controller;
public Home (){
initGUI();
}
public void addHomeListener(ActionListener a){
selectie.addActionListener(a);
}
public void initGUI(){
setLayout(null);
setTitle("");
setPreferredSize(new Dimension(800,600));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel();
label.setBounds(0, 0, 266, 800);
label.setBackground(Color.WHITE);
label.setOpaque(true);
window.add(label);
label1 = new JLabel();
label1.setBounds(267, 0, 266, 800);
label1.setBackground(Color.RED);
label1.setOpaque(true);
window.add(label1);
label2 = new JLabel();
label2.setBounds(533, 0, 266, 800);
label2.setBackground(Color.WHITE);
label2.setOpaque(true);
window.add(label2);
logo = new JButton(new ImageIcon("../Ajax/src/img/logotje.gif"));
logo.setBorderPainted(false);
logo.setBounds(40, 150, 188, 188);
label1.add(logo);
logo1 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
logo1.setBorderPainted(false);
logo1.setBounds(10, 50, 82, 82);
label1.add(logo1);
logo2 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
logo2.setBorderPainted(false);
logo2.setBounds(92, 20, 82, 82);
label1.add(logo2);
logo3 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
logo3.setBorderPainted(false);
logo3.setBounds(174, 50, 82, 82);
label1.add(logo3);
logo4 = new JButton(new ImageIcon("../Ajax/src/img/shirt.png"));
logo4.setBorderPainted(false);
logo4.setBounds(50, 50, 135, 182);
label.add(logo4);
logo5 = new JButton(new ImageIcon("../Ajax/src/img/uitshirt.png"));
logo5.setBorderPainted(false);
logo5.setBounds(65, 50, 138, 190);
label2.add(logo5);
selectie = new JButton("Selectie");
selectie.setBounds(60, 500, 99, 25);
selectie.setActionCommand("selectie");
label.add(selectie);
pack();
/*
* You are making a new object again,
* when you already had declared it as
* an instance Variable. So I used the
* one declared as instance variable..
* To this we will send the object of Home
* class, means the object of this class..
* And as we know that object of the
* class we are in is by default known
* as this, so passing this to HomeController class.
*/
Controller = new HomeController(this);
addHomeListener(Controller);
setVisible(true);
}
public static void main(String... args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new Home();
}
});
}
}
class HomeController implements ActionListener {
/*
* Here we declared a Home class's variable,
* that we will use to dispose that JFrame.
*/
private Home home;
public HomeController(Home home)
{
this.home = home;
}
public void actionPerformed (ActionEvent e){
home.dispose();
Selectie selectie = new Selectie();
selectie.setVisible(true);
}
}
class Selectie extends JFrame
{
public Selectie()
{
initGUI();
}
public void initGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationByPlatform(true);
setSize(300, 300);
}
}
I'd suggest to use CardLayout, most easiest, very confortable for (+1 for SSCCE) your code posted here
never create, re_create bunch of another JFrames, only in the cases that you have got very important reasons, then use JDialog with parent to the JFrame
I am trying to program an 'Inch-to-Centimeter Calculator'. I've got a problem with the method umrechnen(). The label lblCenti cannot be resolved. My Code is equal to the Solutioncode. I am grateful for every answer or tip I get.
I don't know what I should add to my Description, but StackOverflow forces me to write more, so I'm writing this.
package gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class ZollZentimeter extends JFrame {
private JPanel contentPane;
private JTextField tfInch;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ZollZentimeter frame = new ZollZentimeter();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ZollZentimeter() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 359, 157);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnCalculate = new JButton("Umrechnen");
btnCalculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
umrechnen();
}
});
btnCalculate.setBounds(12, 77, 116, 25);
contentPane.add(btnCalculate);
JButton btnEnde = new JButton("Ende");
btnEnde.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnEnde.setBounds(214, 77, 116, 25);
contentPane.add(btnEnde);
JLabel lblZoll = new JLabel("Zoll");
lblZoll.setBounds(12, 13, 56, 16);
contentPane.add(lblZoll);
tfInch = new JTextField();
tfInch.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER){
umrechnen();
}
}
});
JLabel lblCenti = new JLabel("");
lblCenti.setBounds(214, 42, 116, 22);
contentPane.add(lblCenti);
tfInch.setBounds(12, 42, 116, 22);
contentPane.add(tfInch);
tfInch.setColumns(10);
}
private void umrechnen(){
DecimalFormat f=new DecimalFormat("#0.00");
double z, cm;
z=Double.parseDouble(tfInch.getText());
cm=z*2.54;
lblCenti.setText(f.format(cm+" cm"));
tfInch.requestFocus();
tfInch.selectAll();
}
}
JLabel lblCenti = new JLabel("");
"lblCenti" is defined as a local variable so it can only be accessible in the method/constructor where you define it.
It you want to access the label in another method you need to define it as an instance variable, the same way you do with the "tfInch" variable.