如何正确对齐表格 [英] How to properly align the forms

查看:291
本文介绍了如何正确对齐表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何正确提供与这些表格的对齐方式..
检查一下,然后告诉...




How can we properly provide alignment to these forms..
check this and please tell...




import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class MainFrame extends JPanel  {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public MainFrame() {
	      super(true);

	      final JFrame frame = new JFrame("Password Encrypter & Saver");
	      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	      JMenuBar menubar = new JMenuBar();
		  JMenu loginmenu = new JMenu("Login");
		  loginmenu.add(new JSeparator());
		  JMenu registermenu = new JMenu("Registration");
		  registermenu.add(new JSeparator());
		  JMenu manualmenu = new JMenu("Help");
		  manualmenu.add(new JSeparator());
		  
          JMenuItem loginItem1 = new JMenuItem("Login to Project");
                    
		  JMenuItem registerItem1 = new JMenuItem("Register to Project");
		 
		  JMenuItem manualItem1 = new JMenuItem("Manual to use Project");
		  
		  loginmenu.add(loginItem1);
		  registermenu.add(registerItem1);
		  manualmenu.add(manualItem1);
		  
		  menubar.add(loginmenu);
		  menubar.add(registermenu);
		  menubar.add(manualmenu);
		  frame.setJMenuBar(menubar);
		  frame.setSize(500,500);
		  frame.setVisible(true);
		  
		  loginItem1.addActionListener(	new ActionListener() {
		       public void actionPerformed(ActionEvent e) {
		       setVisible(false);
		       new Login();
		      }
		  });
		  
		  registerItem1.addActionListener(	new ActionListener() {
		       public void actionPerformed(ActionEvent e) {
		       setVisible(false);
		       new Registration();
		      }
		  });
		  
		  manualItem1.addActionListener(	new ActionListener() {
		       public void actionPerformed(ActionEvent e) {
		       setVisible(false);
		       new MainFrame();
		      }
		  });
		  
		  
	}  
	
		public static void main(String[] args) {
		new MainFrame();
		/*new Login();
		new Registration();
		new Userframe();
		new Savingframe();
		new Retrievingframe();
		new Showpasswordframe();*/
	}
}





class Login extends JFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final JButton loginbtn;
	final JPanel lgnpanel;
	final JLabel usernameLabel, passwordLabel;
	final JTextField  usernameField;
	final JPasswordField passwordField;
	
	Login()
	{
		super("Login form");
		
		usernameLabel = new JLabel("Username:");
		usernameField = new JTextField(15);

		passwordLabel = new JLabel("Password:");
		passwordField = new JPasswordField(15);

		loginbtn = new JButton("Login");
		
		lgnpanel = new JPanel(new GridLayout(3,2));
		lgnpanel.add(usernameLabel);
		lgnpanel.add(usernameField);
		lgnpanel.add(passwordLabel);
		lgnpanel.add(passwordField);
		lgnpanel.add(loginbtn);
		// to fill the GridLayout
		add(lgnpanel,BorderLayout.CENTER);
		setTitle("LOGIN FRAME");
		setSize(400,200);
		setVisible(true);
		loginbtn.addActionListener(
	    new ActionListener() {
	        public void actionPerformed(ActionEvent e) {
	        // do something for valid login
	        JOptionPane.showMessageDialog(lgnpanel,"Login successful");
	        setVisible(false);
	        new Userframe();
	        }
	    });
		/*loginbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			// do something 
			JOptionPane.showMessageDialog(panel,"Sorry Invalid Username or Password");
			new Login();
			}
	   });*/
	}
}







class Registration extends JFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final JButton submitBtn;
	final JPanel panel;
	final JLabel usernameLabel, passwordLabel,retypepasswordLabel,answerlabel;
	final JTextField  usernameField,combofield,answerfield;
	final JPasswordField passwordField,retypepasswordField;
	final JComboBox combo;
	  
	Registration()
	{
		super("Registration Form");
		
		String course[] = {"Select Security Question","your first crush?","your favourite sportperson?","Your first vehicle number?","Your lucky number?"};
		
		usernameLabel = new JLabel("Choose Username:");
		usernameField = new JTextField(15);

		passwordLabel = new JLabel("Choose Password:");
		passwordField = new JPasswordField(15);
		
		retypepasswordLabel = new JLabel("Retype Password:");
		retypepasswordField = new JPasswordField(15);
		
		combo = new JComboBox(course);
		combo.setBackground(Color.white);
		combo.setForeground(Color.blue);
		
		answerlabel = new JLabel("Answer of Question");
		answerfield = new JTextField(20);

		combofield = new JTextField(15);
		
		submitBtn = new JButton("Submit");
		
		panel = new JPanel(new GridLayout(6,6));
		panel.add(usernameLabel);
		panel.add(usernameField);
		panel.add(passwordLabel);
		panel.add(passwordField);
		panel.add(retypepasswordLabel);
		panel.add(retypepasswordField);
		panel.add(combo);
		panel.add(combofield);
		panel.add(answerlabel);
		panel.add(answerfield);
		panel.add(submitBtn);
		// to fill the GridLayout
		panel.add(new JLabel(""));
		add(panel,BorderLayout.CENTER);
		
		setTitle("REGISTRATION FRAME");
		setSize(400,300);
		setVisible(true);
		
		combo.addItemListener(new ItemListener(){
		public void itemStateChanged(ItemEvent ie){
		String str = (String)combo.getSelectedItem();
		combofield.setText(str);
		  }
		});
		
		submitBtn.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			// do something for login invalid
			JOptionPane.showMessageDialog(panel,"Registration Successful");
			setVisible(false);
			new Login();
			}
	   });

		/*submitBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			// do something 
			JOptionPane.showMessageDialog(panel,"Username already exists or password doesnot match");
			new Registration();
			}
		});*/
		
		
	}
}





class Userframe extends JFrame
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	Userframe()
	{
		final JFrame frame = new JFrame("WELCOME username ");
	    
		//lgnpanel.setVisible(false);
		JMenuBar menubar = new JMenuBar();
		JMenu savemenu = new JMenu("Save");
		JMenu retrievemenu = new JMenu("Retrieve");
		JMenu logoutmenu = new JMenu("LogOut");
		
		JMenuItem saveItem1 = new JMenuItem("Save your Passwords");
        
		JMenuItem retrieveItem1 = new JMenuItem("Retrieve your Passwords");
		 
	    JMenuItem logoutItem1 = new JMenuItem("LogOut of Project");
		  
		savemenu.add(saveItem1);
		retrievemenu.add(retrieveItem1);
		logoutmenu.add(logoutItem1);
		  
		menubar.add(savemenu);
		menubar.add(retrievemenu);
		menubar.add(logoutmenu);
		frame.setJMenuBar(menubar);
		frame.setSize(400,300);
		frame.setVisible(true);
		
		saveItem1.addActionListener(new ActionListener() {
		       public void actionPerformed(ActionEvent e) {
		       setVisible(false);
		       new Savingframe();
		      }
		  });
		
		retrieveItem1.addActionListener(new ActionListener() {
		       public void actionPerformed(ActionEvent e) {
		       setVisible(false);
		       new Retrievingframe();
		      }
		  });
		
		logoutItem1.addActionListener(new ActionListener() {
		       public void actionPerformed(ActionEvent e) {
		       setVisible(false);
		       new MainFrame();
		      }
		  });
	}
}







class Savingframe extends JFrame
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final JButton saveitbtn,addmorebtn;
	final JPanel panel;
	final JLabel usernameLabel,websiteLabel,passwordLabel,creditfieldlabel;
	final JTextField  websiteField,usernameField,creditfield;
	final JPasswordField passwordField;
	
	Savingframe()
	{
		super("Saving Password Frame");
		
		websiteLabel = new JLabel("Website:");
		websiteField = new JTextField(15);

		usernameLabel = new JLabel("Username:");
		usernameField = new JPasswordField(15);
		
		creditfieldlabel = new JLabel("Credit Card Number");
		creditfield = new JTextField(20);
		
		passwordLabel = new JLabel("Password:");
		passwordField = new JPasswordField(15);
		
		saveitbtn = new JButton("Save It");
		addmorebtn = new JButton("Add More");
		
		panel = new JPanel(new GridLayout(6,6));
		panel.add(websiteLabel);
		panel.add(websiteField);
		panel.add(usernameLabel);
		panel.add(usernameField);
		panel.add(passwordLabel);
		panel.add(passwordField);
		panel.add(addmorebtn);
		panel.add(saveitbtn);
		// to fill the GridLayout
		add(panel,BorderLayout.CENTER);

		setTitle("SAVING PASSWORD");
		setSize(400,350);
		setVisible(true);
		
		saveitbtn.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			// do something 
			JOptionPane.showMessageDialog(panel,"Saved Successfully");
			setVisible(false);
			new Userframe();
			}
		});
		
		/*saveitbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			// do something 
			JOptionPane.showMessageDialog(panel,"Website or email-id already exists");
			new Savingframe();
			}
		});*/
	}
}





class Retrievingframe extends JFrame
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final JButton givepasswordbtn;
	final JPanel panel;
	final JLabel usernameLabel,websiteLabel;
	final JTextField  websiteField,usernameField;
	
	Retrievingframe()
	{
		super("Retreving Password Frame");
		
		websiteLabel = new JLabel("Website:");
		websiteField = new JTextField(15);

		usernameLabel = new JLabel("Username:");
		usernameField = new JPasswordField(15);
		
		givepasswordbtn = new JButton("Give Password");
						
		panel = new JPanel(new GridLayout(3,3));
		panel.add(websiteLabel);
		panel.add(websiteField);
		panel.add(usernameLabel);
		panel.add(usernameField);
		panel.add(givepasswordbtn);
		// to fill the GridLayout
		
		add(panel,BorderLayout.CENTER);
		
		setTitle("RETRIEVING PASSWORD");
		setSize(400,150);
		setVisible(true);
		
		givepasswordbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			// do something 
			JOptionPane.showMessageDialog(panel,"Such details does not exists");
			setVisible(false);
			new Retrievingframe();
			}
		});
		
		/*givepasswordbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			// do something 
			new Showpasswordframe();
			}
		});*/
	}
}





class Showpasswordframe extends JFrame
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final JButton continuebtn;
	final JPanel panel;
	final JLabel usernameLabel,websiteLabel,passwordLabel;
	final JTextField  websiteField,usernameField,passwordField;
	
	Showpasswordframe()
	{
		super("Show Password Frame");
		
		websiteLabel = new JLabel("Website:");
		websiteField = new JTextField(15);

		usernameLabel = new JLabel("Username:");
		usernameField = new JPasswordField(15);
		
		passwordLabel = new JLabel("Password:");
		passwordField = new JPasswordField(15);
		
		continuebtn = new JButton("Continue");
				
		panel = new JPanel(new GridLayout(4,3));
		panel.add(websiteLabel);
		panel.add(websiteField);
		panel.add(usernameLabel);
		panel.add(usernameField);
		panel.add(passwordLabel);
		panel.add(passwordField);
		panel.add(continuebtn);
		// to fill the GridLayout
		panel.add(new JLabel(""));
		add(panel,BorderLayout.CENTER);
		
		setTitle("SHOWING YOUR PASSWORD");
		setSize(400,200);
		setVisible(true);
		
		continuebtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			// do something
			setVisible(false); 
			new Userframe();
			}
		});
	}
}

推荐答案

-自定义组件
确定所有组件(标识,要显示的文本)所需的内容,并使其成为基本的实现.

-在GUI旁边设置工作者类
将Action执行委派给工作人员并清理GUI,使其成为唯一的图形表示形式.当需要调整GUI时,您不必触摸逻辑部分.

-设置公共访问接口
限制访问

-设置静态字符串表示形式的接口
仅使用那些公共可用的字符串来确保您使用的引用正确.

-关于属性的东西
为您的应用程序添加属性支持.关于使它们(部分)对用户可用的事情.您还可以仅添加属性以进行某些设置(例如显示或隐藏GUI功能,颜色,语言等).



所以让我们再试一次:

当我弄清半句子的解释正确时,那么您正在寻找一种解决方案,以使这些JFrames代码具有共同的外观.

您已将对象JFrame扩展了几次.也不要编码OOP,这是过程编码.
因此,请分解并扩展自己的JFrame抽象版本:

- customize the components
figure out what you need on all components (ID, text to display) and make it a basic implementation.

- set up worker classes beside the GUI
delegate the Action execution to the worker and clean the GUI to make it a only-graphical representation. When you need to tweak the GUI, you''ll not have to touch the logical parts.

- set up interfaces for public access
limit the access

- set up interfaces for static String representations
use just those public available Strings to make sure you are using the right reference.

- thing about properties
add properties support to your application. Thing about making them (partly) available for the user. You can also add properties only for you to make certain settings (like showing or hiding GUI functions, colors, language, ....).



so let''s try it again:

When I figured your half sentence explanation correct, then you are searching for a solution to have a common look on those JFrames codes.

You have extended the object JFrame a couple of times. Also don''t you code OOP, this is procedural coding.
So break that up and extend an own abstract version of JFrame:

public abstract class BasicFrame extends JFrame {
  //common variables
  // common constructor (with argument ID and Title String of login form)
  public BasicFrame(String strID, String strTitle){
    super(strTitle);
   // ...

  }
  
  public void createForm(/*arguments*/){ // common layout
     createName(/*args*/);
     createPassword(/*args*/);
     create ButtonBar(/**/);
  }

  public void createName(/*args*/){

  }
  public void createPassword(/*args*/){

  }
  public void create ButtonBar(/**/){

  }
}



为确保每个Frame都实现所需的相同方法,可以设置引用这些方法的interface.还可以直接插入ActionListener支持,这使它更加舒适:



to make sure, that every Frame implements the same methods that are needed, you could set up an interface that is referencing those methods. Also can you directly insert the ActionListener support, which makes it even more comfortable:

public interface ILoginFrame extends ActionListener{

 public void createForm(/*args*/);
 public String getPassword();
 // more funny methods


}



那么您的实现可以基于此:



then your implementations can base on that:

class Showpasswordframe extends BasicFrame implements ILoginFrame{

  public Showpasswordframe(){
      super("ShowPassword", "Show Password Frame"); // (ID, title)
    createForm(/*args*/);
  }

  @override
  public createForm(/*args*/){
  // common look of LoginFrames
  }

  @override
  public String getPassword(){
    return null;
  }

// optional  - if needed you can also modify parts of the GUI by changing the basic implementation of the part:
  @ override
  public void create ButtonBar(/*args*/){
    // different Button bar
  }

  // and of course the actions:
  @override
  public void actionPerformed(ActionEvent oEvent) {
  if(null != getPassword()){
    // party!
  }
  //.... the other methods 
}



我希望这是您要寻找的.否则,我们将需要2-3个句子来描述您的问题.



I hope this is what you were searching for. Otherwise we''ll need 2-3 sentences in which you describe your problem more.


这篇关于如何正确对齐表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆