Hallo zusammen,
Hab ein Problem mit JLabel und zwar bekomme ich es nicht hin, den Text links starten zu lassen. Sollte ja eigentlich der Standard sein, bei mir stehts aber egal was ich tue in der Mitte.
Code wie folgt:
Gruß Rolli
Hab ein Problem mit JLabel und zwar bekomme ich es nicht hin, den Text links starten zu lassen. Sollte ja eigentlich der Standard sein, bei mir stehts aber egal was ich tue in der Mitte.
Code wie folgt:
Java:
package gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.xml.parsers.ParserConfigurationException;
import modules.Heise;
import org.xml.sax.SAXException;
public class Gui {
// instance vars
private final static String DEFAULT_LOOK_AND_FEEL = new String(
UIManager.getSystemLookAndFeelClassName());
private JFrame frame;
private JPanel panel;
private JComboBox selection;
private JTable table;
private JLabel statusBar;
// constructor
public Gui() {
frame = new JFrame("My first GUI :)");
// set system look and feel
try {
UIManager.setLookAndFeel(DEFAULT_LOOK_AND_FEEL);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(frame);
// settings
frame.setSize(600, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
// create components
// panel
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
// combobox
selection = new JComboBox();
selection.addItem(new String("Please choose..."));
// statusbar
statusBar = new JLabel();
statusBar.setPreferredSize(new Dimension(50, 20));
statusBar.setHorizontalTextPosition(SwingConstants.LEFT);
statusBar.setHorizontalAlignment(SwingConstants.LEFT);
// table
table = new JTable();
JScrollPane scrollPane = new JScrollPane(table);
table.setTableHeader(null);
// menu
JMenuBar mBar = new JMenuBar();
JMenu mFile = new JMenu("File");
JMenu mView = new JMenu("View");
ButtonGroup lafGroup = new ButtonGroup();
JMenuItem fileExit = new JMenuItem("Exit");
/*
* text shown in menu is a human readable name of the lookAndFeel. can
* be retrieved with getText() classname of the lookAndFeel can be
* retrieved with getName()
*/
for (int i = 0; i < UIManager.getInstalledLookAndFeels().length; i++) {
JRadioButtonMenuItem button = new JRadioButtonMenuItem(
UIManager.getInstalledLookAndFeels()[i].getName());
button.setName(UIManager.getInstalledLookAndFeels()[i]
.getClassName());
lafGroup.add(button);
mView.add(button);
button.addActionListener(new LookAndFeelSelector());
if (button.getName().equals(DEFAULT_LOOK_AND_FEEL)) {
lafGroup.setSelected(button.getModel(), true);
}
}
mBar.add(mFile);
mBar.add(mView);
mFile.add(fileExit);
// listener
fileExit.addActionListener(new MenuFileExit());
// frame
frame.setJMenuBar(mBar);
frame.add(BorderLayout.CENTER, scrollPane);
frame.add(BorderLayout.SOUTH, panel);
panel.add(selection);
panel.add(statusBar);
frame.setVisible(true);
}
// LISTENER
private class MenuFileExit implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
private class LookAndFeelSelector implements ActionListener {
public void actionPerformed(ActionEvent event) {
// get button object
JRadioButtonMenuItem button = (JRadioButtonMenuItem) event
.getSource();
try {
UIManager.setLookAndFeel(button.getName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(frame);
}
}
public class Selection implements ActionListener {
public void actionPerformed(ActionEvent event) {
try {
table.setModel(new TableModel(new Heise().deliver()));
statusBar.setText("Done...");
} catch (ParserConfigurationException e) {
statusBar.setText("Error parsing Feed...");
e.printStackTrace();
} catch (SAXException e) {
statusBar.setText("Error parsing Feed...");
e.printStackTrace();
} catch (MalformedURLException e) {
statusBar.setText("Error fetching Data... (Malformed URL)");
e.printStackTrace();
} catch (IOException e) {
statusBar.setText("Error parsing Feed...");
e.printStackTrace();
}
}
}
// GETTER / SETTER
public JFrame getFrame() {
return frame;
}
public JComboBox getSelection() {
return selection;
}
}
Gruß Rolli