㈠ javascript中,如何让button按钮处于页面最中,而不是页面上部居中,求代码,谢谢
<!DOCTYPEhtml>
<htmllang="zh-CN">
<head>
<metacharset="UTF-8">
<title>按钮绝对居中</title>
<style>
.button{
position:fixed;
left:50%;
top:50%;
transform:translate(-50%,-50%);
}
</style>
</head>
<body>
<buttonclass="button">居中的按钮</button>
</body>
</html>
㈡ java怎样放两个按钮在窗体的正中间
JPanel 放入到BorderLayout.CENTER , 那么会自动填充满整个contentPane的中间, 而JPanel内部还是流式布局, 一行排满 自动换到下一行,从上到下. 所以按钮还是在最上面.
(把JPanel的背景色改成蓝色,就可以清晰的看到JPanel填满了窗口)
importjavax.swing.*;
{
publicJFDemo2(){
JPanelpane=newJPanel();
BoxLayoutlayout=newBoxLayout(pane,BoxLayout.X_AXIS);//水平的盒布局
pane.setLayout(layout);
JButtonmessageButton=newJButton("OK");
JButtoncloseButton=newJButton("Cancel");
pane.add(Box.createGlue());//挤占ok按钮和窗口左侧空间
pane.add(messageButton);
pane.add(Box.createHorizontalStrut(20));//按钮之间的水平距离
pane.add(closeButton);
pane.add(Box.createGlue());//挤占cancel按钮和窗口右侧空间
add(pane);
setTitle("Demo");//标题
setSize(320,230);//窗口大小
setLocationRelativeTo(null);//窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//窗口点击关闭时,退出程序
}
publicstaticvoidmain(String[]args){
newJFDemo2().setVisible(true);
}
}
总结: 推荐使用方法二,使用盒布局来实现.
一般不推荐使用绝对布局/空布局 来布局窗口, 因为不同的操作系统下显示的效果不完全一致.
并且还需要写大量的代码来计算组件的大小和位置, 当窗口放大和缩小时 还需要重新计算位置
㈢ 这个JAVA窗体怎么居中求大神指点!!!!!
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("Black_box");
setSize(WIDTH, HEIGHT);
}
public static final int WIDTH = 600;
public static final int HEIGHT = 400;
}
改成
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("Black_box");
Toolkit toolkit=Toolkit.getDefaultToolkit();
int x=(toolkit.getScreenSize().width+WIDTH)/2;
int y=(toolkit.getScreenSize().height+HEIGHT)/2;
setBounds(x, y, WIDTH, HEIGHT);
}
public static final int WIDTH = 600;
public static final int HEIGHT = 400;
这样就可以实现窗体居中。
}
㈣ button里面的字怎么居中啊
关键,文字要包含在label标签中,并设置行高,否则文字会与文本框的顶
端对齐。文本框要设置vertical-align:middle;否则文本框与button顶端对
齐。button中文字垂直居中,要设置高和行高,行高要小于高。
<html>
<head>
<style type='text/css'>
LABEL
{
LINE-HEIGHT: 20px;
HEIGHT: 20px
}
.button
{
font-size:12px;
text-align:center;
padding:0px;
vertical-align:middle ;
line-height:22px;
margin:0px;
Height:26px;
Width:60px;
}
.txt
{
border:1px #6699CC solid;
height:20px;
width:160px;
margin:0px;
vertical-align:middle;
font-size:12px;
padding:0px 2px;
line-height :16px;
}
</style>
</head>
<body>
div中文字,文本框,button按钮垂直居中对齐方法及的css样式。
<div class="divPad" style="FONT-SIZE: 12px; HEIGHT: 28px;">
<label>查询:企业名称</label><input ID="txt_company"
type="text" class="txt" />
<label>姓名</label><input ID="txt_name"
class="txt" Width="80px" type="text"/>
<input type="button" ID="Button1" value="查找"
class="button" /></div>
</body>
</html>
㈤ 请问在java JTable中如何设置单元格字体居中对齐
这是三个类,你重新整一下哟!package database.other;import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;public class MainFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = 7094024210608765600L;
private UserModel model;
private JTextField txtName = new JTextField(10);
private JTextField txtPWD = new JTextField(10);
private JButton btnAdd = new JButton("添加");public MainFrame() {
super("数据库表格演示");
this.setLayout(new BorderLayout());
//添加Table
model = new UserModel();
JTable table = new JTable(model);
table.setFont(new Font(null, Font.PLAIN, 20));
table.getTableHeader().setFont(new Font(null, Font.BOLD, 22));
this.add(new JScrollPane(table), BorderLayout.CENTER);
//添加数据相关控件
JPanel panel = new JPanel(new FlowLayout());
panel.add(new JLabel("姓名:"));
panel.add(txtName);
panel.add(new JLabel("密码:"));
panel.add(txtPWD);
panel.add(btnAdd);
this.add(panel, BorderLayout.PAGE_END);
//事件绑定
btnAdd.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setResizable(false);
this.setVisible(true);
}@Override
public void actionPerformed(ActionEvent e) {
boolean result = model.addUser(txtName.getText(), txtPWD.getText());
if (!result) {
JOptionPane.showMessageDialog(this, "数据添加失败!");
}
}public static void main(String[] args) {
new MainFrame();
}
}
package database.other;public class UserInfo {
private int userID;
private String userName;
private String passWord;public UserInfo() {
}public int getUserID() {
return userID;
}public void setUserID(int userID) {
this.userID = userID;
}public String getUserName() {
return userName;
}public void setUserName(String userName) {
this.userName = userName;
}public String getPassWord() {
return passWord;
}public void setPassWord(String passWord) {
this.passWord = passWord;
}@Override
public String toString() {
return String.format("%d,%s,%s", userID, userName, passWord);
}
}
package database.other;import database.one.SqlConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;public class UserModel extends AbstractTableModel {
private static final long serialVersionUID = 3488161650785725386L;
private ArrayList<UserInfo> users = new ArrayList<UserInfo>();
private SqlConnection sc;public UserModel() {
super();
sc = new SqlConnection();
init();
}@Override
public int getColumnCount() {
return 3;
}@Override
public int getRowCount() {
return users.size();
}@Override
public Object getValueAt(int row, int column) {
UserInfo user = users.get(row);
switch (column) {
case 0:
return user.getUserID();
case 1:
return user.getUserName();
case 2:
return user.getPassWord();
default:
return null;
}
}@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "用户编号";
case 1:
return "用户名称";
case 2:
return "用户密码";
default:
return super.getColumnName(column);
}
}private void init() {
//初始化数据
Connection conn = sc.getConnection();
String sql = "select * from UserInfo";
try {
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery(sql);
while (rs.next()) { //读取数据到集合中
UserInfo user = new UserInfo();
user.setUserID(rs.getInt(1));
user.setUserName(rs.getString(2));
user.setPassWord(rs.getString(3));
users.add(user);
}
}
catch (SQLException e) {
System.out.printf("%s%n", e.getMessage());
}
finally {
try {
conn.close();
}
catch (SQLException e) {
}
}
}public boolean addUser(String userName, String passWord) {
//添加用户
Connection conn = sc.getConnection();
String sql = "insert into UserInfo (UserName,PassWord) values (?,?)";
PreparedStatement ps;
UserInfo user = new UserInfo();
user.setUserName(userName);
user.setPassWord(passWord);
try {
ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, userName);
ps.setString(2, passWord);
int x = ps.executeUpdate();
if (x == 1) {
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
user.setUserID(rs.getInt(1));
}
users.add(user);
fireTableDataChanged();
return true;
}
return false;
}
catch (SQLException e) {
System.out.printf("%s%n", e.getMessage());
return false;
}
finally {
try {
conn.close();
}
catch (SQLException e) {
}
}
}
}
回答完毕,希望对你的提问有帮助,如果满意请采纳o(∩_∩)o...哈哈