我的JTextField
和JLabel
在JPanel
中不透明当JTable
可见时,它会在JTextField
和JLabel
之间重叠。我想在搜索内容(JTable
)时显示JTextField
,但它重叠且JTable
无法正常显示。请在这里帮助我的代码:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.border.BevelBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import com.alee.laf.WebLookAndFeel;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JTable;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JScrollPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLayeredPane;
public class ButtonBarExample1 {
private JFrame frame;
private JTextField Search;
private JTable CategoryTable;
private DefaultTableModel model;
private static Connection con;
private String query;
private PreparedStatement PStat;
private ResultSet res;
private JScrollPane scrollPane;
private int Enter=0;
private JPanel panel;
private JLabel label;
private JLabel label_1;
private JTextField CategoryID;
private JTextField CategoryName;
private JPanel panel_1;
/**
* Launch the application.
*/
public static void main(String\[\] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WebLookAndFeel.install();
ButtonBarExample1 window = new ButtonBarExample1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ButtonBarExample1() {
initialize();
con=Database.Database();
}
public void remove(){
while(CategoryTable.getRowCount()>0) {
model.removeRow(0);
}
}
public void changed(){
if(Search.getText().length()==0){
panel.remove(scrollPane);
panel.repaint();
panel.revalidate();
}else{
try{
panel.add(scrollPane);
scrollPane.setViewportView(CategoryTable);
remove();
query="Select * from CategoryEntry where Category_Name like '"+Search.getText().trim()+"%'";
PStat=con.prepareStatement(query);
res=PStat.executeQuery();
model=(DefaultTableModel)CategoryTable.getModel();
model.setRowCount(0);
CategoryTable.setRowHeight(30);
while(res.next()){
String CatID=res.getString("Category_ID");
String CatName=res.getString("Category_Name");
Object\[\] row= {CatID,CatName};
model.addRow(row);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
PStat.close();
res.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 934, 601);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 917, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 562, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
panel.setLayout(null);
Search = new JTextField();
Search.getDocument().addDocumentListener(new DocumentListener(){
public void changedUpdate(DocumentEvent e){
changed();
}
public void removeUpdate(DocumentEvent e){
changed();
}
public void insertUpdate(DocumentEvent e){
changed();
}
});
Search.setBounds(90, 11, 402, 31);
panel.add(Search);
Search.setColumns(10);
scrollPane = new JScrollPane();
scrollPane.setBounds(90, 41, 402, 127);
CategoryTable = new JTable();
CategoryTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
int row=CategoryTable.getSelectedRow();
String TableClicked=(CategoryTable.getModel().getValueAt(row, 0)).toString();
try{
query="Select * from CategoryEntry where Category_ID='"+TableClicked+"'";
PStat=con.prepareStatement(query);
res=PStat.executeQuery();
if(res.next()){
String CatID=res.getString("Category_ID");
CategoryID.setText(CatID);
String CatName=res.getString("Category_Name");
CategoryName.setText(CatName);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
PStat.close();
res.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
});
CategoryTable.setModel(new DefaultTableModel(
new Object\[\]\[\] {
},
new String\[\] {
"CategoryID", "CategoryName"
}
));
JLabel lblNewLabel = new JLabel("Search");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
lblNewLabel.setBounds(21, 19, 46, 14);
panel.add(lblNewLabel);
panel_1 = new JPanel();
panel_1.setBounds(31, 53, 317, 76);
panel_1.setOpaque(false);
panel.add(panel_1);
panel_1.setLayout(null);
label_1 = new JLabel("Category ID");
label_1.setBounds(10, 14, 97, 18);
panel_1.add(label_1);
label_1.setFont(new Font("Tahoma", Font.BOLD, 11));
CategoryID = new JTextField();
CategoryID.setBounds(117, 11, 157, 25);
CategoryID.setBackground(new Color(255,255,255,128));
CategoryID.setOpaque(false);
panel_1.add(CategoryID);
CategoryID.setEditable(false);
CategoryID.setColumns(10);
label = new JLabel("Category Name");
label.setBounds(10, 42, 97, 18);
panel_1.add(label);
label.setFont(new Font("Tahoma", Font.BOLD, 11));
CategoryName = new JTextField();
CategoryName.setBounds(117, 39, 157, 25);
panel_1.add(CategoryName);
CategoryName.setEditable(false);
CategoryName.setColumns(10);
frame.getContentPane().setLayout(groupLayout);
}
}
答案 0 :(得分:0)
我已经解决了。实际上我将scrollpane透明度设置为true,并使其视口透明度为true,如此。
scrollPane.setOpaque (true);
scrollPane.getViewPort ().setOpaque (true);