I've been searching and nothing has been simple enough for me. But for the thing I'm working on I want to make one JFrame in my main method and use different class files for each JPanel. (This is to keep the information separated and clean).
Also what's the best way to switch JPanels if I do it in this method?
public class Main extends JFrame {
public Main(){
JFrame intro = new JFrame("FormProgram");
intro.setSize(800,600);
intro.setVisible(true);
intro.setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
intro.setLocationRelativeTo(null);
}
public static void main(String[] args) {
new Main();
}
Then my basic second class looks like this.
public class Page1 extends JFrame implements ActionListener{
public JLabel test;
public void Page1(){
Container cp = intro.getContentPane();
cp.setLayout(null);
this.test = new JLabel("welcome");
this.test.setBounds(5,5,300,300);
cp.add(test);
}
what's the best way to switch JPanels
Use a CardLayout. Read the section from the Swing tutorial on How to Use a CardLayout for more information and a working example.
Related
so I got this in my Main class:
public class Main extends JFrame {
public static void main(String[] args) {
JFrame Launch = new JFrame();
Launch.setSize(800, 400);
Launch.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Launch.setContentPane(new StartView());
Launch.setTitle("De Hartige Hap");
Launch.setVisible(true);
}
}
Now let's say I'm in that Panel ("StartView()")
and onClick on a button, I want to change the frames' contentpane..
How would I do this?
public class StartView extends javax.swing.JPanel {
public StartView() {
initComponents();
}
private void OrderButtonActionPerformed(java.awt.event.ActionEvent evt) {
/*instead of Launch.setContentPane(new StartView());
*it has to be (new otherView())
*/
}
Pass your Launch object to your panel object (i.e. new StartView(launch)). This way, you can create a method changeView() in Launch and you can call this method from your panel (launch.changeView()), you can change your view inside that method.
Also, if I may adivce you, take a look at the ModelViewController pattern. This makes sure you keep the View (your panels) and the Controller (your Frame) seperated so you don't get issues like this.
I'm kind of confused. I'm at work right now (just started apprenticeship) and need to create a fully editable table (I'll use SQL soon). So I have 2 questions here:
What do you mean by "Don't extend JFrame"? Let's say I have a class called "TestDialog" and also a JFrame which is called "TestUI". Would it be okay to write
public class TestDialog extends TestUI ?
As I have understood it, one shouldn't create a class (called MyExample) and inside of this class just write
public class MyExample extends JFrame
Because you create a JFrame within an existing class instead of creating it seperate.
I'll keep it short - Can I use 2 actions in 1 listener (for 1 button)? Something like:
public void actionPerformed(ActionEvent e)
{
Action_One; Action_Two;
}
Or do I need to use 2 different listeners?
Okay that's it I guess. I'm sorry that I haven't written everything clearly, I just registered here and actually concentrate on translating things from my language into english. If anyone could tell me how to write here like in Eclipse I'd appreciate it, because I couldn't really find out how.
Composition over inheritance is an important programming approach. So I preffer following construction of GUI.
public class Application {
private JFrame mainFrame;
private MainPanel mainPanel;
private void installFrame() {
// initialize main frame
mainFrame = new JFrame("Title");
}
private void installComponents() {
// install all components
mainPanel = new MainPanel();
}
private void layout() {
// provide layouting
mainFrame.add(mainPanel.getComponent());
}
private void show() {
mainFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Application app = new Application();
app.installFrame();
app.installComponents();
app.layout();
app.show();
}
});
}
}
Main panel has no inheritance from JPanel, but use an instance of it.
public class MainPanel {
private JPanel mainPanel;
public MainPanel() {
mainPanel = new JPanel(new GridBagLayout()); // or another layout
initComponents();
layout();
}
private void initComponents() {
// init all components here
}
private void layout() {
// layout panel here
}
public Component getComponent() {
return mainPanel;
}
}
The same pattern I use for each complex component (for example for trees, tables, lists, tabbed panes etc.). But this approach has one disadvantage: there is no GUI builder that support it.
About actions: you can provide a combined action. Something like this
public class CombinedAction extends AbstractAction {
private Action[] delegates;
public CombinedAction(String name, Icon icon, Action... someDelegates) {
super(name, icon);
delegates = someDelegates;
}
public void actionPerformed(ActionEvent ae) {
for (Action delegate : delegates) {
delegate.actionPerfromed(ae);
}
}
}
What do you mean 2 actions? You can do anything in the actionPerformed() method. If instead your question is "can I have 2 actionPerformed methods in the class" ie two actionListeners then it's a 'NO'. You must instead read on Inner Classes to do that.
And regarding your first question it's more of a design issue.
I would prefer
public class Example{
JFrame frame;
public void initialize()
{
frame.setSize(//params);
frame......
//other frame initilizing code
}
public static void main(string[] args)
{
Example example=new Example();
example.initialize();
}
}//class ends
while someone else may feel otherwise
I have drawn a rectangle using a JPanel
My main objective is to store my Requirement Engineering chapter into a JPanel or a JFrame
import java.awt.*;
import javax.swing.*;
class RequirementEngineering extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent();
g.drawRect(10,10,60,60);
g2.drawString("Feasibility study", 20, 20); //rectangle is my main objective, I will look after the string later
}
public static void main(String[] args)
{
}
}
how do I display the JPanel? I know, JApplet doesn't require a main method, but how do we represent JPanel in main() method?
I have this doubt for long, and other posts are confusing me further, Could I have a direct question
My main question being "How to add JFrame to JPanel" pertaining my current coding
thanks in advance
see if you need to use a Window based app, you can do as:
JPanel customPanel = new RequirementEngineering();
JFrame frame = new JFrame("my window");
frame.getContentPane().add(customPanel );
frame.setSize(300,200);
frame.setVisible(true);
If you need in Applet,
public class JAppletExample extends JApplet {
public void init() {
Container content = getContentPane();
JPanel customPanel = new RequirementEngineering();
content.add(customPanel );
}
And you can run it using appletViewer or in any Web Browser such as IE.
I am working on a video game in Java that so far has a main menu class and a class for the game. As I have it set up right now, each class uses its own JFrame, which means that when the user clicks "start game", the main menu JFrame closes and the games JFrame opens. Obviously this is not ideal and I would like to have both classes use the same JFrame, however I really don't know how to go about this and internet searches have not been helpful.
The class for my main menu is:
public class Frame extends javax.swing.JFrame {
...
}
I have it set up right now so that my Game class imports my Frame class, but when I try to make the JFrame display elements from my game nothing comes up. So my question is:
How do I use one single JFrame across multiple classes?
Any help is much appreciated!
Rather then needing to pass a reference of the main frame to each of the child panels, which might expose parts of the program you don't want them to have access to (as an example), you should use something like a CardLayout and use the main frame as the main display hub, switching out the panels as you need to
Check out How to use CardLayout for more examples
Rather than having each class be its own frame, you can have one frame, with several classes manipulating it. I would probably set something up like this:
public class MainFrame extends JFrame {
public MainFrame() {
super("Cool Game!");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class Game {
private final MainFrame mainFrame;
public Game(final MainFrame mainFrame) {
this.mainFrame = mainFrame;
mainFrame.setContentPane(createGamePanel());
}
private JPanel createGamePanel() {
//...
}
}
public class MainMenu {
private final MainFrame mainFrame;
public MainMenu(final MainFrame mainFrame) {
this.mainFrame = mainFrame;
}
public void showMainMenu() {
mainFrame.setContentPane(createMainMenuPanel());
}
private JPanel createMainMenuPanel() {
//...
}
}
You should use just one frame at overall game. And there should be many JPanels for different contents.
Deciding content and switching should be like this :
switch( currentState ) {
case introduction:
setContentPane(new IntroductionPanel());
break;
case insideGame:
setContentPane( new GamePanel() );
...
...
...
}
Use cardLayout to switch panels
http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html
I have this class for my UI
public class MyFrame extends JFrame{
JTextArea textArea;
public MyFrame(){
setSize(100,100);
textArea = new JTextArea(50,50);
Container content = getContentPane();
content.add(textArea);
}
public static void main(String[] args){
JFrame frame = new MyFrame();
frame.show();
UpdateText u = new UpdateText();
u.settext("Helloworld");
}
}
And I have this another class that will set the text of textArea, in which I extended MyFrame to access textArea in another class.
public class UpdateText extends MyFrame{
public void settext(String msg){
textArea.setText(msg);
}
}
Then I instantiate UpdateText and call the function settext. but the text doesn't seem to appear in the GUI.
First of all, don't override the setText() method unless you want different behavior. Second of all, you don't have to extend anything. All you have to do is follow these simple steps and you'll be set!
In the UpdateText class, put these lines somewhere in it:
MyFrame gui;
public UpdateText(MyFrame in) {
gui = in;
}
In the 'MyFrame` class, put this line at the beginning:
UpdateText ut = new UpdateText(this);
Now, you can refer to everything in the MyFrame class from the UpdateText class by preceeding what you want to change with gui. For example, say you wanted to change the text of your textarea. The code would be the following:
gui.textArea.setText("Works!");
Happy coding! :)