gpt4 book ai didi

swing - 如何在 java swing 中设置样式?

转载 作者:行者123 更新时间:2023-11-30 09:46:15 24 4
gpt4 key购买 nike

我是 Java 的新手,我想创建一个程序,但在设计它的样式时遇到了一些问题。我想要一个具有以下结构的简单程序:

第一行应该是带有文件和关于按钮的菜单栏。在第二行我应该有一个文本:“url:”,一个文本输入(用于 url)和一个文本输入后的“go”按钮。在第三行,我需要一个包含 3 个选项卡的选项卡式菜单:tab1、tab2、tab3。

直到窗口底部的其余内容应该是一个有行和列的表格,如果内容太大或者窗口比表格大而不应该填充,它应该有垂直和水平滚动条整个窗口。

目前我只有这段代码:

主要.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);
}
}

菜单栏.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){
}
}
}

最佳答案

这就是我所理解的您所描述的布局示例(我使用 NetBeans 所见即所得 - 很好的工具,但无论如何您都应该了解布局)。它不使用您的代码,但可以让您基本了解如何完成它。我建议您在此处阅读有关布局的更多信息 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();
}

关于swing - 如何在 java swing 中设置样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7221440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com