我试图在java swing中构建小应用程序。我开始使用此链接:Creating a Grid in Java。
一切都很好。我完成了那个任务,但是我正试图删除多余的标题边框。我试图减少标题边框的边界仍然不影响。任何有良好建议的人都试着与我分享。
package swings.application.framework;
import java.awt.*;
import java.awt.event.ActionEvent
;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Inf3rNix
*/
public class KidsProblemTable {
private static JButton okbtn;
private static JFrame frame;
public static void main(String[] args) {
Runnable runner = new Runnable() {
@Override
public void run() {
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("Kids Table");
Border border = LineBorder.createGrayLineBorder();
frame.setLayout(null);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
JLabel Heading = new JLabel("My Result");
Heading.setFont(new Font("Gabriola", Font.ITALIC, 56));
Heading.setForeground(Color.BLUE);
Heading.setBounds(550, -60, 300, 200);
JLabel Heading2 = new JLabel("Nice Job! Here are your results...");
Heading2.setFont(new Font("Buxton Sketch", Font.ITALIC, 26));
Heading2.setForeground(Color.BLUE);
Heading2.setBounds(500, 10, 600, 200);
JLabel Heading3 = new JLabel();
String str = "You got 5 correct answer out of 7 question";
Heading3.setText("<html><a href=' ' style='color:rgb(248,116,49);'>" + str + "</a></html>");
Heading3.setFont(new Font("Andy", Font.PLAIN, 20));
Heading3.setBounds(500, 70, 600, 200);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
Border border1 = BorderFactory.createTitledBorder("Exercise");
panel1.setBounds(150, 200, 1000, 190);
String column = "Questions".toUpperCase();
// AttributedString as = new AttributedString(column);
// Font plainFont = new Font("Times New Roman", Font.PLAIN, 24);
// as.addAttribute(TextAttribute.FONT, plainFont);
// as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON,0,8 );
String column1 = "Problem".toUpperCase();
String column2 = "Answer".toUpperCase();
String column3 = "Answer".toUpperCase();
String results = "results".toUpperCase();
Object rowData[][] = {{"1", "3+9", "12", "12", "Correct !"},
{"2", "4+3", "7", "7", "Correct !"},
{"3", "10+8", "17", "18", "Sorry :("},
{"4", "5+1", "6", "6", "Correct !"},
{"5", "10+8", "18", "18", "Correct !"},
{"6", "8+2", "10", "10", "Correct !"},
{"7", "5+6", "4", "11", "Sorry :("}
};
Object columnNames[] = {column, column1, column2, column3, results};
DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
JTable tb1 = new JTable(model) {
//when checking if a cell is editable always return false
public boolean isCellEditable(int rowIndex, int colIndex) {
return false; //Disallow the editing of any cell
}
};
//JTable table1 = new JTable(rowData, columnNames1);
tb1.setBackground(Color.getHSBColor(153, 0, 91));
JScrollPane scrollPane1 = new JScrollPane(tb1);
Border raisedbevel = BorderFactory.createRaisedBevelBorder();
Border loweredbevel = BorderFactory.createLoweredBevelBorder();
Border raisedetched = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
// ImageIcon icon = createImageIcon("images/wavy.gif", "wavy-line border icon");
okbtn=new JButton("Press Ok to Exit");
okbtn.setBorder(raisedbevel);
okbtn.setFont(new Font("Vani", Font.BOLD, 12));
okbtn.setBounds(575, 390, 150, 50);
panel2.add(scrollPane1, BorderLayout.CENTER);
okbtn.addActionListener(new buttonActionListener());
frame.add(Heading);
frame.add(Heading2);
frame.add(Heading3);
frame.add(okbtn);
frame.add(panel1);
panel2.setBorder(border1);
panel1.add(panel2);
frame.setSize(500, 500);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
private static class buttonActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==okbtn){
JOptionPane.showMessageDialog(null, "All the best !!!");
WindowEvent winClosingEvent = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING );
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent( winClosingEvent );
}
}
}
}
答案 0 :(得分:2)
也许
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Inf3rNix
*/
public class KidsProblemTable {
private JFrame frame;
private JPanel panel1 = new JPanel();
private JLabel Heading = new JLabel("My Result", JLabel.CENTER);
private JLabel Heading2 = new JLabel("Nice Job! Here are your results...", JLabel.CENTER);
private JLabel Heading3 = new JLabel();
private String str = "You got 5 correct answer out of 7 question";
private JPanel panel2 = new JPanel();
private JButton okbtn;
private Object rowData[][] = {{"1", "3+9", "12", "12", "Correct !"},
{"2", "4+3", "7", "7", "Correct !"},
{"3", "10+8", "17", "18", "Sorry :("},
{"4", "5+1", "6", "6", "Correct !"},
{"5", "10+8", "18", "18", "Correct !"},
{"6", "8+2", "10", "10", "Correct !"},
{"7", "5+6", "4", "11", "Sorry :("}
};
private String column = "Questions".toUpperCase();
private String column1 = "Problem".toUpperCase();
private String column2 = "Answer".toUpperCase();
private String column3 = "Answer".toUpperCase();
private String results = "results".toUpperCase();
private Object columnNames[] = {column, column1, column2, column3, results};
private DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
private JTable tb1 = new JTable(model) {
private static final long serialVersionUID = 1L;
//when checking if a cell is editable always return false
@Override
public boolean isCellEditable(int rowIndex, int colIndex) {
return false; //Disallow the editing of any cell
}
};
private JScrollPane scrollPane1 = new JScrollPane(tb1);
public KidsProblemTable() {
Border border1 = BorderFactory.createTitledBorder("Exercise");
Border raisedbevel = BorderFactory.createRaisedBevelBorder();
tb1.setBackground(Color.getHSBColor(153, 0, 91));
tb1.setPreferredScrollableViewportSize(tb1.getPreferredSize());
Heading.setFont(new Font("Gabriola", Font.ITALIC, 56));
Heading.setForeground(Color.BLUE);
Heading2.setFont(new Font("Buxton Sketch", Font.ITALIC, 26));
Heading2.setForeground(Color.BLUE);
Heading3.setText("<html><a href=' ' style='color:rgb(248,116,49);'>" + str + "</a></html>");
Heading3.setFont(new Font("Andy", Font.PLAIN, 20));
panel1.setLayout(new GridLayout(3, 1));
panel1.add(Heading);
panel1.add(Heading2);
panel1.add(Heading3);
// ImageIcon icon = createImageIcon("images/wavy.gif", "wavy-line border icon");
okbtn = new JButton(" Press Ok to Exit ");
okbtn.setBorder(raisedbevel);
okbtn.setFont(new Font("Vani", Font.BOLD, 12));
okbtn.addActionListener(new buttonActionListener());
panel2.add(okbtn);
panel2.setBorder(border1);
frame = new JFrame("Kids Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel1, BorderLayout.NORTH);
frame.add(scrollPane1, BorderLayout.CENTER);
frame.add(panel2, BorderLayout.SOUTH);
frame.pack();
frame.setSize(500, 500);
frame.setVisible(true);
}
private class buttonActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == okbtn) {
JOptionPane.showMessageDialog(null, "All the best !!!");
WindowEvent winClosingEvent = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
}
}
}
public static void main(String[] args) {
Runnable runner = new Runnable() {
@Override
public void run() {
new KidsProblemTable();
}
};
EventQueue.invokeLater(runner);
}
}
答案 1 :(得分:1)
我找到了你的问题, 您将scrollPane添加到panel2为
panel2.add(scrollPane1, BorderLayout.CENTER);
但你没有将setLayout设置为panel2的BorderLayout。 并将panel1的布局设置为null 看到这段代码
JPanel panel1 = new JPanel();
panel1.setLayout(null);//add to your code
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());//add to your code
panel2.setBounds(0,0,800,120);//add to your code
out put:
可能会在几小时后删除此图片 out put image