java gui 选项_GUI中的Java选项卡

package landscape;

import javax.swing.*;

import java.awt.Component;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.*;

import java.text.DecimalFormat;

public class Landscape extends JFrame {

private JFrame mainFrame;

private JButton calculateButton;

private JButton exitButton;

private JTextField lengthField;

private JLabel lengthLabel;

private JTextField widthField;

private JLabel widthLabel;

private JTextField depthField;

private JLabel depthLabel;

private JTextField volumeField;

private JLabel volumeLabel;

private JRadioButton builtIn;

private JRadioButton above;

private JTabbedPane panel;

public Landscape()

{

JTabbedPane tabs=new JTabbedPane();

tabs.addTab("Pool", panel);//add a tab for the panel with the title "title"

//you can add more tabs in the same fashion - obviously you can change the title

//tabs.addTab("another tab", comp);//where comp is a Component that will occupy the tab

mainFrame.setContentPane(tabs);//the JFrame will now display the tabbed pane

mainFrame.setSize(265,200);

mainFrame.setLocationRelativeTo(null);

mainFrame.setVisible(true);

mainFrame.setResizable(false);

JPanel panel = new JPanel();//FlowLayout is default

//Pool

panel.setOpaque(false);//this tells the panel not to draw its background; looks nicer under LAFs where the background inside a tab is different from that of JPanel

panel.add(builtIn);

panel.add(above);

panel.add(lengthLabel);

panel.add(lengthField);

panel.add(widthLabel);

panel.add(widthField);

panel.add(depthLabel);

panel.add(depthField);

panel.add(volumeLabel);

panel.add(volumeField);

panel.add(calculateButton);

panel.add(exitButton);

//creating components

calculateButton = new JButton ("Calculate");

exitButton = new JButton ("Exit");

lengthField = new JTextField (5);

lengthLabel = new JLabel ("Enter the length of your pool: ");

widthField = new JTextField (5);

widthLabel = new JLabel ("Enter the width of your pool: ");

depthField = new JTextField (5);

depthLabel = new JLabel ("Enter the depth of your pool: ");

volumeField = new JTextField (5);

volumeLabel = new JLabel ("Volume of the pool: ");

//radio button

ButtonGroup buttonGroup = new ButtonGroup();

builtIn = new JRadioButton ("Built in");

buttonGroup.add(builtIn);

above = new JRadioButton ("Above");

buttonGroup.add(above);

exitButton.setMnemonic('x');

calculateButton.setMnemonic('C');

mainFrame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e)

{ System.exit(0); }

});

// create the handlers

calculateButtonHandler chandler = new calculateButtonHandler(); //instantiate new object

calculateButton.addActionListener(chandler); // add event listener

ExitButtonHandler ehandler = new ExitButtonHandler();

exitButton.addActionListener(ehandler);

FocusHandler fhandler = new FocusHandler();

lengthField.addFocusListener(fhandler);

widthField.addFocusListener(fhandler);

depthField.addFocusListener(fhandler);

}

class calculateButtonHandler implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

DecimalFormat num = new DecimalFormat(", ###.##");

double width;

double length;

double depth;

double volume;

double volume2;

double height;

String instring;

instring = lengthField.getText();

if (instring.equals(""))

{

instring = "0";

lengthField.setText("0");

}

length = Double.parseDouble(instring);

instring = widthField.getText();

if (instring.equals(""))

{

instring = "0";

widthField.setText("0");

}

width = Double.parseDouble(instring);

instring = depthField.getText();

if (instring.equals(""))

{

instring = "0";

depthField.setText("0");

}

depth = Double.parseDouble(instring);

volume = width * length * depth;

volumeField.setText(num.format(volume));

volume2 = width * length * depth;

}

}

class ExitButtonHandler implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

}

class FocusHandler implements FocusListener

{

public void focusGained(FocusEvent e)

{

if(e.getSource() == lengthField || e.getSource() == widthField || e.getSource() == depthField)

{

volumeField.setText("");

}

else if (e.getSource() == volumeField)

{

volumeField.setNextFocusableComponent(calculateButton);

calculateButton.grabFocus();

}

}

public void focusLost1(FocusEvent e)

{

if(e.getSource() == widthField)

{

widthField.setNextFocusableComponent(calculateButton);

}

}

public void focusLost(FocusEvent e)

{

if(e.getSource() == depthField)

{

depthField.setNextFocusableComponent(calculateButton);

}

}

}

public static void main(String args[])

{

Landscape app = new Landscape();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
javaGUI图形界面 public class login extends JFrame { private JComboBox nameJComboBox; private JPanel userJPanel; private JLabel pictureJLabel; private JButton okJButton,cancelJButton; private JLabel nameJLabel,passwordJLabel,note; private JPasswordField passwordJPasswordField; private String name1; private String password1; private String user; private ImageIcon myImageIcon; public login( ) { createUserInterface(); // 调用创建用户界面方法 } private void createUserInterface() { Container contentPane = getContentPane(); contentPane.setLayout( null ); userJPanel = new JPanel(); userJPanel.setBounds( 35, 120, 300, 96 ); userJPanel.setBorder(BorderFactory.createEtchedBorder() ); //显示一圈边儿 userJPanel.setLayout( null ); contentPane.add( userJPanel ); nameJComboBox = new JComboBox(); nameJComboBox.setBounds( 100, 12, 170, 25 ); nameJComboBox.addItem( "admin" ); nameJComboBox.addItem( "aloie" ); nameJComboBox.setSelectedIndex( 0 ); nameJComboBox.setEditable(true); userJPanel.add( nameJComboBox ); pictureJLabel=new JLabel(); pictureJLabel.setBounds(45,0,380,118); pictureJLabel.setIcon(new ImageIcon("pic.gif")); contentPane.add(pictureJLabel); nameJLabel=new JLabel("姓 名:"); nameJLabel.setBounds(20,12,80,25); userJPanel.add(nameJLabel); passwordJPasswordField=new JPasswordField(); passwordJPasswordField.setBounds(100,60,170,25); userJPanel.add(passwordJPasswordField); passwordJLabel=new JLabel("密 码:"); passwordJLabel.setBounds(20,60,80,25); userJPanel.add(passwordJLabel); note=new JLabel("密码与用户名相同"); note.setBounds(0,295,180,25); add(note); okJButton=new JButton("登 陆"); okJButton.setBounds(60,250,80,25); contentPane.add(okJButton); okJButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { okJButtonActionPerformed(event); } } ); cancelJButton=new JButton("取 消"); cancelJButton.setBounds(210,250,80,25); contentPane.add(cancelJButton); cancelJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit(0); //退出登陆 } } ); setTitle( "登陆窗口" ); setSize( 380, 350 ); setResizable( false ); //将最大化按钮设置为不可用 } private void okJButtonActionPerformed( ActionEvent event ) { //okJButton响应事件,检查用户名和密码的匹配 name1= nameJComboBox.getSelectedItem().toString(); if (name1.equals("admin") ) { if (passwordJPasswordField.getText().equals("admin")) { showNewWindow(); setVisible( false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } else if (name1.equals("aloie")) { if ( passwordJPasswordField.getText().equals("aloie") ) { showNewWindow(); setVisible(false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } } public void showNewWindow() { JFrame jf=new JFrame("main Frame"); jf.setSize(500,400); jf.setVisible(true); jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public static void main( String[] args ) { JFrame.setDefaultLookAndFeelDecorated(true); login mylogin = new login( ); mylogin.setVisible( true ); mylogin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值