I found this code on the internet. I am setting up a new Eclipse on my new laptop and I want to be able to open this in design view.
How do you open a class with JComponents' on it in design view and also is there a way to make that the default?
Seems like and easy thing but I have been looking for this in Eclipse for the better part of an our. I thought it was in Open With... but I didn't see anything that that sounded like design view.
I hope this is a good question because I just cannot simple find the design view button.
code:
package TestMenu;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
public class StartupWindow extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
private JButton btn;
public StartupWindow()
{
super("Simple GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btn = new JButton("Open the other JFrame!");
btn.addActionListener(this);
btn.setActionCommand("Open");
add(btn);
pack();
}
@Override
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if(cmd.equals("Open"))
{
dispose();
new AnotherJFrame();
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run()
{
new StartupWindow().setVisible(true);
}
});
}
}
class AnotherJFrame extends JFrame
{
private static final long serialVersionUID = 1L;
public AnotherJFrame()
{
super("Another GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JLabel("Empty JFrame"));
pack();
setVisible(true);
}
}
解决方案
If by "design view" you mean the Eclipse Windowbuilder, you can open that with "Open with ..." > "WindowBuilder Editor." However, I have tested your code with a Kepler SR1 Eclipse on Mac OS X, and got the following error message in the Design View, which I guess means that you won't be able to use it in the Design View as is.
The parser parsed the compilation unit, but can't identify any GUI toolkit, so WindowBuilder can't display any GUI to edit.
In your case I'd try to create a new WindowBuilder class from the wizard, and re-create the class from scratch, e.g., in the Design View.
In case you cannot find the option WindowBuilder Editor in the Open with ... menu at all, perhaps you'll need to install WindowBuilder in your new Eclipse instance first. To do this, go to https://www.eclipse.org/windowbuilder/download.php, pick the link to the release version update site for your version of Eclipse (the one for Kepler SR1 is http://download.eclipse.org/windowbuilder/WB/release/R201309271200/4.3/), and install via "Install New Software" (or follow the instructions from the update site link, which includes comprehensive installation details).