- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 JDialog,其中显示 Jtable,JTable 的单元格是一个打开新 JFrame (CoveNew) 的 JButton:
public void birthstats(String stat) {
//System.out.println(classeMain);
String titolo=new String();
if ("nascite".equals(stat)){
titolo=lingua_corrente.getString("nascite_title");
elencojLabel.setText(titolo);
}
else{
titolo=lingua_corrente.getString("anella");
elencojLabel.setText(titolo);
}
Dimension dimensioni = new Dimension(500, 700);
nascitejDialog.setSize(dimensioni);
nascitejDialog.setTitle("GesAll 1.0 - " + titolo);
nascitejDialog.setModal(true);
hatchlistijTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null}
},
new String [] {
"Num", lingua_corrente.getString("coppiaLabel"), lingua_corrente.getString("annojLabel"), lingua_corrente.getString("jLabel8"), lingua_corrente.getString("NascitaLabel")
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class, JButton.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, false
};
@Override
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
hatchlistijTable.setShowGrid(true);
hatchlistijTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
hatchlistijTable.getColumnModel().getColumn(0).setHeaderValue("Num."); //Numero
hatchlistijTable.getColumnModel().getColumn(0).setResizable(false);
hatchlistijTable.getColumnModel().getColumn(0).setPreferredWidth(60);
hatchlistijTable.getColumnModel().getColumn(1).setHeaderValue(lingua_corrente.getString("coppiaLabel")); //coppia
hatchlistijTable.getColumnModel().getColumn(1).setResizable(false);
hatchlistijTable.getColumnModel().getColumn(1).setPreferredWidth(50);
hatchlistijTable.getColumnModel().getColumn(2).setHeaderValue(lingua_corrente.getString("annojLabel")); //anno
hatchlistijTable.getColumnModel().getColumn(2).setResizable(false);
hatchlistijTable.getColumnModel().getColumn(2).setPreferredWidth(50);
hatchlistijTable.getColumnModel().getColumn(3).setHeaderValue(lingua_corrente.getString("jLabel8")); //gabbia
hatchlistijTable.getColumnModel().getColumn(3).setResizable(false);
hatchlistijTable.getColumnModel().getColumn(3).setPreferredWidth(50);
if ("nascite".equals(stat)){
hatchlistijTable.getColumnModel().getColumn(4).setHeaderValue(lingua_corrente.getString("schiusa")); //data nascita prevista
}
else{
hatchlistijTable.getColumnModel().getColumn(4).setHeaderValue(lingua_corrente.getString("NascitaLabel")); //data nascita
}
hatchlistijTable.getColumnModel().getColumn(4).setResizable(false);
hatchlistijTable.getColumnModel().getColumn(4).setPreferredWidth(150);
hatchlistijTable.setRowHeight(25);
//hatchlistijTable.getColumnModel().getColumn(5).setHeaderValue("Jump"); //salto
//hatchlistijTable.getColumnModel().getColumn(5).setResizable(false);
//hatchlistijTable.getColumnModel().getColumn(5).setPreferredWidth(50);
for (int i=hatchlistijTable.getRowCount()-1;i>=0;i--)
((javax.swing.table.DefaultTableModel) hatchlistijTable.getModel()).removeRow(i);
String qDep = "SELECT d FROM Deposizioni d WHERE d.fecondo=" + '"' + 'S' +'"' +" AND d.morto=" + '"' + 'N' +'"' + " AND d.dataSchiusa is null ORDER by d.dataDeposizione ASC";
if (!"nascite".equals(stat))
qDep = "SELECT d FROM Deposizioni d WHERE d.morto=" + '"' + 'N' +'"' + " AND d.anello is null" + " AND d.dataSchiusa is not null ORDER by d.dataSchiusa ASC";
List depo = DBgateway.getInstance().Run_query(qDep);
System.out.println(depo.size());
//Deposizioni dd = (Deposizioni) DBgateway.getInstance().Run_query(qDep);
for (int i=0;i<depo.size();i++){
//Deposizioni dd = (Deposizioni) DBgateway.getInstance().Run_query(qDep).get(i);
Deposizioni dd = (Deposizioni) depo.get(i);
int toAdd = Program.getConf().getSchiusa();
Calendar cal = Calendar.getInstance();
if ("nascite".equals(stat)){
cal.setTime(dd.getDataDeposizione());
}
else {
toAdd = Program.getConf().getAnellare();
cal.setTime(dd.getDataSchiusa());
}
Calendar today=Calendar.getInstance();
cal.add(Calendar.DATE, toAdd); // add n days
//if(today.after(cal)){
System.out.println(toAdd + " " + today.after(cal) +" " + "nascite".equals(stat));
Object [] rowData=getRowData(dd,i, cal,stat);
if("nascite".equals(stat))
((javax.swing.table.DefaultTableModel) hatchlistijTable.getModel()).addRow(rowData);
else
if(today.after(cal) && !"nascite".equals(stat))
((javax.swing.table.DefaultTableModel) hatchlistijTable.getModel()).addRow(rowData);
}
TableCellRenderer buttonRenderer = new JTableButtonRenderer();
hatchlistijTable.getColumn(lingua_corrente.getString("jLabel8")).setCellRenderer(buttonRenderer);
hatchlistijTable.addMouseListener(new JTableButtonMousePriListener(hatchlistijTable));
nascitejDialog.setVisible(true);
}
private Object[] getRowData(Deposizioni dd,int i, Calendar cal, final String stat) {
try {
String qCp="SELECT c FROM Coppie c WHERE c.idCoppia=" + findDatiCova(dd.getIdDeposizione()).getIdCoppia();
Coppie rCp=(Coppie) DBgateway.getInstance().Run_query(qCp).get(0);
String data = df.format(cal.getTime());
//Bottone salto cova
final JButton jump=new JButton(rCp.getGabbia().toString());
jump.addActionListener(new java.awt.event.ActionListener() {
private Object classeMain = this.getClass();
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jumpActionPerformed(evt);
}
private void jumpActionPerformed(ActionEvent evt) {
System.out.println("jump" +jump.getText().toString());
String qS ="SELECT c.idCoppia from Coppie c WHERE c.attiva=" + "'"+"S"+"'" + " AND c.gabbia="+jump.getText().toString();
CoveNew cova = new CoveNew(mainClass, (Integer) DBgateway.getInstance().Run_query(qS).get(0),stat);
cova.setVisible(true);
nascitejDialog.setVisible(false);
mainClass.setVisible(false);
}
});
//
Object [] rowData = {i+1, // progressivogianluca
rCp.getNumeroCoppia(), // numero coppia
rCp.getAnno(),
//rCp.getGabbia(), // fecondo
jump,
data};
return(rowData);
} catch (IOException ex) {
Logger.getLogger(Main_menu.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
private static class JTableButtonMousePriListener extends MouseAdapter {
private final JTable table;
public JTableButtonMousePriListener(JTable table) {
this.table = table;
}
@Override public void mouseClicked(MouseEvent e) {
int column = table.getColumnModel().getColumnIndexAtX(e.getX());
int row = e.getY()/table.getRowHeight();
if (row < table.getRowCount() && row >= 0 && column < table.getColumnCount() && column >= 0) {
Object value = table.getValueAt(row, column);
if (value instanceof JButton) {
((JButton)value).doClick();
//if(((JButton)value).isEnabled())
// anella(row,column);
}
}
}
}
JTable 正确显示,我遇到的问题是,当我第一次单击 Jbutton 时,新的 JFrame 将显示一次,如果我再次单击,它将显示 7 的 3 倍,依此类推。你能帮我只展示一次吗?谢谢吉安卢卡。
最佳答案
您不应该将 JButton 添加到 TableModel。 JTable 使用呈现器和编辑器来显示数据。
参见Table Button Column一种允许您在列中存储文本但随后对该列使用自定义渲染器/编辑器的方法,使其看起来和操作就像一个按钮。您所需要做的就是在单击单元格时提供一个操作。
关于java - Jbutton 执行更多操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21677969/
本质上,我编写的是一个益智游戏。 它包含一个图像,该图像进一步分为 9 block ,放置在包含 3x3 JButton GridLayout 的 JPanel 上。最初,9 个按钮是空的。当用户点击
我正在制作一个游戏,其中有一个名为 move(int x1, int y1, int x2, int y2) 的方法,该方法将棋子从第一个按钮 (x1,y1) 移动并将其放置在第二个按钮 (x2, y
我使用 NetBeans 创建了一个框架。该框架有两个按钮 A 和 B。按钮 A 最初被禁用。仅当单击按钮 B 时才启用。 public newFrame() { //newFrame is t
我想使用 MouseListener 从按钮矩阵中删除某个按钮,并在空白处添加 JLabel,所以我使用: import java.awt.*; import java.awt.event.*; im
我正在创建一个黑白棋游戏,想知道如何在单击时将按钮文本从 B 更改为 W,反之亦然。 我尝试将 Action 监听器添加到我的 forloop 中,这为游戏创建了我的板,当我单击标记为 W 的按钮时,
我的项目中有 7 个按钮。其中有 6 个类别,RandomSoru 按钮是随机选择其中一个类别的按钮。我想访问所选类别。 “r”是随机生成器。 RandomSoru.addActionListener
我对此感到好奇,因为我们正在 Swing 中制作游戏,并且出于某种原因将 map 图 block 制作为 jButtons 而不是 jPanels。现在我们想将单位放在它们上面,这样当单位位于它们上面
我创建了一个按钮数组: JButton bt[][]=new JButton[8][8]; 然后我通过以下方式调用一个名为 refreshBoard 的函数 public void refreshBo
我想在这篇文章的序言中告诉大家,这是我第一次发帖,所以如果有任何错误,请告诉我。 我正在尝试为我的高中计算机数学(编程)课编写一款二十一点游戏,但出现了很多错误,但真正令人烦恼的是这个错误。 我的 G
我正在创建一个“谁想成为百万富翁”游戏,并创建了一个半按钮,我想使用它来删除两个 JButton 答案。以下是两个作为答案选项的 JButton 的代码。 enter code here: Answe
我有一个 JButton,我想在按下它时创建一个新的 JButton,我添加了一个如下所示的 ActionListener,但它没有添加另一个 JButton。 public void actionP
如果在 JTextBox 中输入字符串,然后按“输入按钮”,它会在 JTextField 中返回正确的数据,如果我更改字符串,然后重新按输入,也会返回正确的数据。如果我按“输入按钮”然后按“添加”按钮
我想在单击另一个 jbutton 时获得一个 jbutton。 Here the link for sample code(Log in as jbutton,asdf as a password)
假设我创建了一个带有 jbuttons 的 2d tile map ,然后在 map 顶部创建了单位,当单位(也是一个 jbutton)位于 tile 顶部时,有没有办法显示 map 的背景,因为如何
我正在开发一个项目,我需要单击一个按钮来创建另一个按钮。最终,我希望对新按钮的位置有更多的控制,并能够多次创建新按钮,但现在......我希望只让一个 JButton 创建另一个 JButton。 使
JPanel 中有 12 个 JButton。我想单击其中一个并禁用所有其他(它将“突出显示”)。然后,如果我单击禁用的一个,则前一个将被禁用,并且过程是相同的。考虑到有 12 个按钮并且我想避免重复
我正忙于用 Java 制作游戏。然而,我正在努力让 GUI 正常工作。 我遇到的问题: 当按下按钮时,我希望它显示一个彩色圆圈,当再次按下按钮时,我希望它在旧圆圈周围画一个圆圈,这可能会发生,直到每个
大家好,我正在尝试使用 Java 创建一个简单的国际象棋游戏。我有一个 [8][8] 数组的 JButton 板。我为每个分配一个新的 SquareListener,它是一个扩展 BoardListn
我有 JButtons“暂停”和“取消暂停”。当用户暂停程序时,应禁用“暂停”按钮,并启用“取消暂停”按钮。我不知道怎么写。取消暂停按钮有效,但暂停按钮不起作用,因为“无法解决取消暂停”。怎么处理呢?
我设置了一个网格布局,中间有 16 个按钮。我在第一个按钮上放置了一个图标。 我将如何循环,当用户选择网格上的下一个按钮时,它将图标从旧位置 move 到新位置? private ArrayList
我是一名优秀的程序员,十分优秀!