开发者

How to style in java swing?

I`m very new to Java and I want to create a program and I already have some problems styling it. I want a simple program with the following structure:

The first line should be the menubar with file and about buttons. In the second line I should have a text: "url: ", a text input (开发者_开发问答for the url) and a "go" button after the text input. On the third line I need a tabbed menu with 3 tabs: tab1, tab2, tab3.

The rest of the content until the bottom of the window should be a table with rows and columns and it should have vertical and horizontal scroll bars if the content is too big and if the window is bigger than the table than it should fill the entire window.

For now I have only this code:

main.java :

import javax.swing.*;

public class main {

    public static void main(String args[]){

        JFrame frame = new JFrame("SEO Tool");      
        frame.setSize(500,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        frame.setVisible(true);

        menubar menubar = new menubar(); 
        menubar.createmenubar_site(frame);

        tabs tabs = new tabs();
        tabs.createtabs(frame); 
    }
}

tabs.java :

import javax.swing.*;
import javax.swing.BoxLayout;

public class tabs {

    public void createtabs(JFrame frame){

        JLabel sitelabel = new JLabel("A");

        JPanel sitepanel = new JPanel();
        sitepanel.setLayout(new BoxLayout(sitepanel, BoxLayout.X_AXIS));
        sitepanel.setSize(400,400);
        sitepanel.add(sitelabel);
        frame.add(sitepanel);

        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        //panel.setBounds();
        frame.add(panel);

        JTabbedPane tabs = new JTabbedPane();

        JPanel yoursite = new JPanel();
        JPanel yourcompetitors = new JPanel();

        tabs.addTab("Your site",yoursite);
        tabs.addTab("Your competitors",yourcompetitors);

        panel.add(tabs);        

        sitetab sitetab = new sitetab();
        sitetab.createcontent(yoursite);    
    }
}

menubar.java :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class menubar {

    public void createmenubar_site(JFrame frame){

        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);

        JMenu file = new JMenu("File");
        menubar.add(file);

        JMenuItem newproject = new JMenuItem("New Project");
        newproject .addActionListener(new click_newproject());
        file.add(newproject);
    }

    static class click_newproject implements ActionListener {

        public void actionPerformed (ActionEvent e){            
        }
    }
}


That's the example of layout you described as i understood (i made it using NetBeans WYSIWYG - good tool, but you should understand layouts anyway). It doesn't use your code but can give you basic understanding of how it can be done. I would recommend you read more about layouts here http://download.oracle.com/javase/tutorial/uiswing/layout/using.html .

private void initComponents() {

    urlPanel = new javax.swing.JPanel();
    urlLabel = new javax.swing.JLabel();
    urlField = new javax.swing.JTextField();
    urlButton = new javax.swing.JButton();
    tabbedPane = new javax.swing.JTabbedPane();
    tab1 = new javax.swing.JPanel();
    tab2 = new javax.swing.JPanel();
    tab3 = new javax.swing.JPanel();
    tableScrollPane = new javax.swing.JScrollPane();
    table = new javax.swing.JTable();
    mainMenu = new javax.swing.JMenuBar();
    fileMenu = new javax.swing.JMenu();
    aboutMenu = new javax.swing.JMenu();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

    urlPanel.setLayout(new java.awt.BorderLayout());

    urlLabel.setText("url:");
    urlPanel.add(urlLabel, java.awt.BorderLayout.WEST);

    urlField.setText("http://google.com");
    urlPanel.add(urlField, java.awt.BorderLayout.CENTER);

    urlButton.setText("Go");
    urlPanel.add(urlButton, java.awt.BorderLayout.EAST);

    getContentPane().add(urlPanel);

    javax.swing.GroupLayout tab1Layout = new javax.swing.GroupLayout(tab1);
    tab1.setLayout(tab1Layout);
    tab1Layout.setHorizontalGroup(
        tab1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 395, Short.MAX_VALUE)
    );
    tab1Layout.setVerticalGroup(
        tab1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 32, Short.MAX_VALUE)
    );

    tabbedPane.addTab("tab1", tab1);

    javax.swing.GroupLayout tab2Layout = new javax.swing.GroupLayout(tab2);
    tab2.setLayout(tab2Layout);
    tab2Layout.setHorizontalGroup(
        tab2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 395, Short.MAX_VALUE)
    );
    tab2Layout.setVerticalGroup(
        tab2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 32, Short.MAX_VALUE)
    );

    tabbedPane.addTab("tab2", tab2);

    javax.swing.GroupLayout tab3Layout = new javax.swing.GroupLayout(tab3);
    tab3.setLayout(tab3Layout);
    tab3Layout.setHorizontalGroup(
        tab3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 395, Short.MAX_VALUE)
    );
    tab3Layout.setVerticalGroup(
        tab3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 32, Short.MAX_VALUE)
    );

    tabbedPane.addTab("tab3", tab3);

    getContentPane().add(tabbedPane);

    tableScrollPane.setViewportView(table);

    getContentPane().add(tableScrollPane);

    fileMenu.setText("File");
    mainMenu.add(fileMenu);

    aboutMenu.setText("About");
    mainMenu.add(aboutMenu);

    setJMenuBar(mainMenu);

    pack();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜